Amazon | Online Screener 2021 | Minimum Moves For Equal Array Sums
Anonymous User
857

Given 2 arrays, return the minimum number of moves needed in order to make the sums of both arrays equal, or return -1 if this is not possible.

Ex. 1: Given A = [5], B = [1, 1, 6], should return 1, as you turn the 6 into a 3 in array B, then the sums will be equal --> 5 = 1 + 1 + 3

Ex. 2: Given A = [2, 3, 1, 1, 2], B = [5, 4, 6], should return 2, as you turn 4 and 6 in B to 1 and 3, and then the sums are equal --> 2 + 3 + 1 + 1 + 2 = 5 + 1 + 3

Ex. 3: Given A = [5, 4, 1, 2, 6, 5], B = [2], should return 6, as you turn everything in A to 1, 1, 1, 1, 1, 1 and B to 6, so the sums are equal --> 1 + 1 + 1 + 1 + 1 + 1 = 6

Ex. 4: Given A = [1, 2, 3, 4, 3, 2, 1], B = [6], should return -1, not possible to have the same sums.

Constraints: Size of A and B are [1...100,000] and each element belongs in the range of [1..6] only.


Anyone have any ideas? Preferably in Python if possible. Is this a DP question? I'm familiar with Minimum Cost aka BFS, and I'm also familiar with the similar question of getting 2 arrays to match.

But this seems like neither... some help or code would be appreciated!

Comments (6)