Get Minimum Operations
The owner of HackerMall loves organized items. A row of items is organized if the parity (even or odd) is different for each adjacent stack of items. To organize the row, half of the items in any stack can be removed. This can happen as many times and on as many stacks as is required. Determine the minimum number of operations needed to organize a row.
More formally, given an array items[] of integer of length n, the array is organized if for each x less than n-1, items[x] mod 2 != items[x + 1] mod 2. A mod B is the remainder of A divided by B. In one operation, the owner can choose an element and divide it by 2. That is, if one chooses index x then do items[x] = floor( items[x]/2). The goal is to return the minimum number of operations that one needs to perform to organize the array.
Complete the function getMinimumOperations in the editor below.
getMinimumOperations has the following parameter(s):
int items[n]: a row of stacks of items
Returns
int the minimum number of operations needed to organize the array
♫⋆。♪ ˚♬ ゚.°ᡣ𐭩 Credit to ㄇE ʚଓ
1Example 1
items[2] mod 2 = items[3] mod 2.
One way to organize the array is shown using 1-based indexing.
2Example 2
items[2] mod 2 = items[3] mod 2.
Here is the way to make items organized in 3 moves.
1. Choose the 3rd index and divide it by 2;
the new is [6, 5, 4, 7, 3].
2. Choose the 5th index and divide it by 2;
items = [6, 5, 4, 7, 1].
3. Choose the 5th index and divide it by 2;
items = [6, 5, 4, 7, 0] :)Constraints
Limits and guarantees your solution can rely on.
1 <= n <= 10^51 <= items[i] <= 2^30