Get Minimum Operations
Learn this problemProblem statement
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.
Function
getMinimumOperations(items: int[]) → int
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 ʚଓ
Examples
Example 1
items = [4, 10, 10, 6, 2]return = 2items[2] mod 2 = items[3] mod 2.
One way to organize the array is shown using 1-based indexing.
Example 2
items = [6, 5, 9, 7, 3]return = 3items[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
1 <= n <= 10^51 <= items[i] <= 2^30
More JPMorgan Chase problems
- Bitwise XOR SubsequencesOA · Seen Jul 2026
- Array ChallengeOA · Seen Jun 2026
- Minimum Cores to Handle ProcessesOA · Seen Jun 2026
- About ShippingOA · Seen Jun 2026
- Count Dropped RequestsOA · Seen Jan 2026
- Generate Table of ContentsOA · Seen Jan 2026
- Calculate Net ProfitSeen Jun 2025
- Find Total WeightSeen Jun 2025