I encountered a question that I couldn't solve during my algorithm interview. The question goes:
Given an array of length n, where n is an even number, regroup the elements in the array into two smaller arrays, each with equal length (n/2), with the condition that the sum of each of these smaller arrays is equal. If this cannot be achieved, return -1.
Eg. Input: [1,1,1,2,3,3,3,6] Output: [[1,1,2,6], [1,3,3,3]]
Eg. Input: [1,4,5,6] Output: -1
I was thinking about randomly initializing two subarrays and then interchanging two elements so that their difference is at least half the total differences in the sum of two arrays. However, I had trouble coming up with a criterion that would dictate the scenario when the input is illegitimate, and the answer should be -1.
Thanks for any help and hints!