Description
Solutions
Submission
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.

Function Description

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

Example 1:

Input:  items = [4, 10, 10, 6, 2]
Output: 2
Explanation:
The array is not organized since, for example, items[2] mod 2 = items[3] mod 2. One way to organize the array is shown using 1-based indexing.
  • Choose the 2nd index and divide it by 2; the new array is [4, 5, 10, 6, 2].
  • Choose the 4th index and divide it by 2; the new array is [4, 5, 10, 3, 2]. [4, 5, 10, 3, 2] is an organized array so return the number of operations, 2.
  • Constraints:
      Unknown for now
    Thumbnail 0
    Testcase

    Result
    Case 1

    input:

    output: