As reported: Google L3 phone screen, question 2 (December 2024): given an array of
nelements, in one operation delete an element and twice that element (the value2x); find the minimum number of moves to make the array empty.
You are given an array nums of positive integers. In one move you delete one element x, and if the array also contains the value 2x, you may delete one copy of 2x in the same move.
Return the minimum number of moves needed to delete every element.
Example
nums = [1, 2, 4, 8] -> 2 (1, 2) and (4, 8)
nums = [2, 4, 4, 8] -> 2 (2, 4) and (4, 8)
nums = [3, 6, 12] -> 2 (3, 6), then 12 alone — or 3 alone, then (6, 12)
nums = [1, 1, 2] -> 2
nums = [5, 7] -> 2
Clarifications
- A move may delete a single element even when its double is present.
- Values may repeat; each copy is a separate element.
Constraints: 1 ≤ n ≤ 2·10^5; 1 ≤ nums[i] ≤ 10^9.
Code runner format: the first line is n, the second line holds the array. Implement minMoves(nums); the harness prints the result.