As reported: IBM on-campus online assessment (HackerRank, 70 minutes, two coding problems, 2026). Problem 1: make the array alternating in parity.
You are given an array nums of positive integers. In one operation you choose an index i and replace nums[i] with floor(nums[i] / 2). The same element may be chosen any number of times.
Return the minimum number of operations needed to make the parities alternate, in either of the two patterns:
- odd, even, odd, even, …
- even, odd, even, odd, …
Example 1
nums = [6,12,5,10] -> 1
Pattern odd, even, odd, even: only 6 is wrong; 6 → 3 takes 1 operation. Pattern even, odd, even, odd: 12 → 6 → 3 (2), 5 → 2 (1), 10 → 5 (1) = 4 operations. The answer is the smaller, 1.
Example 2
nums = [3,12,5,10] -> 0
The array already alternates odd, even, odd, even.
Constraints: 1 ≤ n < 10^5; 1 ≤ nums[i] ≤ 10^9.
Code runner format: one JSON value per line: nums. Implement minOperations(nums); the harness prints the number.