I had problems with the interview when I came across an interesting one that I could not think of a solution to. The tasks indicated:
Create a function that takes an array of integers. The last two numbers in this array are "a" and "b". The function should find if all numbers in the array, when they are added / subtracted in any way, are equal to mod b, except for the last two numbers a and b.
So, let's say we have an array:
array = [5, 4, 3, 3, 1, 3, 5].
I need to find out if there is a possible “placement” in this array +/-so that the numbers can equal 3 mod 5. The function must print True for this array, because 5+4-3+3-1 = 8 = 3 mod 5.
An “obvious” and simple solution would be to try to add / subtract everything in all possible ways, but this is a complex integrated solution, possibly O (2 n ).
Is there any way to make this better?
Edit: The question requires the function to use all numbers in the array, not any. Except, of course, for the last two.
source
share