Greedy Approach I
On a first glance, a greedy approach for this problem might seem intractable, but it isn't. The fact that the numbers are all unique really help here. Bucket them up into two sorted sets, one for even and one for odd numbers. Then greedily try even first, then odd. When trying even, you can quickly calculate for each number whether there is a possibility to use the second rule. The same for the odd case. When doing that, despite being a greedy approach (trying all possibilities), it runs very quickly in nlogn (a linear approach should be feasible too). Code is down below, cheers, ACC. Construct Uniform Parity Array II - LeetCode ou are given an array nums1 of n distinct integers. You want to construct another array nums2 of length n such that the elements in nums2 are either all odd or all even . For each index i , you must choose exactly one of the following (in any order): nums2[i] = nums1[i] ...