Give an array of arrays. I’d like to generate all arrays formed by picking the first element from the first array in the input, the second element in the second array and so on.
The order in which these arrays show up is important. The first array should be composed of the first elements of all the arrays in the input. The next array should be such that the sum of absolute distances from the first array should be the smallest possible. The third array generated should have the second smallest sum of absolute distances from the first one and so on.
As an example, consider the input below.
[array([11, 9, 13, 7, 15, 6, 16, 4, 18, 2, 20, 0, 22]),
array([4, 3, 5, 2, 6, 1, 7, 0, 8]),
array([ 9, 7, 11, 6, 12, 5, 13, 4, 14, 2, 16, 0, 18])]
The first array should be [11,4,9] (sum of absolute differences is 0). The second array should be [11,3,9] (sum of absolute differences is 1). The third array should be either of [9,4,9] or [11,4,7] (sum of absolute differences is 2) and so on.
One way to do this is to generate all possible combinations with simple back-tracking and then sort them by absolute distance from the first array. But this will be very inefficient, memory wise. I don’t need to store all the arrays and just need to iterate over them.
And the simple backtracking approach will explore arrays in the order where the first priority is to keep the first element 11 and the next priority is to keep the second element at 4 and so on.
1