Smoothing a pair of arrays Input

Two lists with binary values (list_a, list_b) of equal length=>3.

Output

Two lists with binary values (smoothed_a, smoothed_b) with minimal Forward Difference while keeping the same sum before and after processing sum(list_a) + sum(list_b) == sum(smoothed_a) + sum(smoothed_b).

Forward Difference being the absolute difference between items. For example:


[0, 1, 0] would be sum(abs([1, -1])) =&gt; 2
[1, 1, 1] would be sum(abs([0, 0])) =&gt; 0


Illustrative test cases

Formatted as (list_a, list_b) =&gt; (smoothed_a, smoothed_b)

([0, 0, 1, 0, 0],
 [1, 1, 0, 1, 1])
=&gt;
([0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1])


The sum of both arrays pairs is 5. The forward difference was minimized to 0.

([1, 0, 1, 0, 1],
 [0, 0, 0, 1, 0])
=&gt;
([0, 1, 1, 1, 1],
 [0, 0, 0, 0, 0])


The sum of both arrays pairs is 4. The forward difference was minimized to 1.

Test cases

Formatted as (list_a, list_b): forward_diff =&gt; (smoothed_a, smoothed_b): min_diff where (smoothed_a, smoothed_b) are potential solutions.

([0, 0, 1],
 [0, 1, 0]): 3
([0, 0, 0],
 [0, 1, 1]): 1

([1, 1, 1],
 [0, 0, 0]): 0
([1, 1, 1],
 [0, 0, 0]): 0

([0, 1, 0],
 [1, 0, 1]): 4
([0, 0, 0],
 [1, 1, 1]): 0

([1, 1, 1, 1],
 [0, 0, 0, 0]): 0
([1, 1, 1, 1],
 [0, 0, 0, 0]): 0

([1, 0, 1, 1],
 [0, 0, 0, 1]): 3
([1, 1, 1, 1],
 [0, 0, 0, 0]): 0

([1, 0, 0, 1],
 [0, 0, 0, 1]): 2
([1, 1, 1, 0],
 [0, 0, 0, 0]): 1

([1, 1, 0, 1],
 [1, 0, 1, 0]): 3
([1, 1, 1, 1],
 [1, 0, 0, 0]): 1
Jelly, 3 bytes

FṢṁ


Try it online!

Explanation:


Flatten the list 
Ṣort the flattened list
ṁold the sorted flatten list in the shape of the input.