The owners of HackerMall loves organized items. A row of items is organized if the…
- User
The owners 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 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 length n, the array is organized if for each x less than n-1, items[x] % 2 != items[x + 1] % 2. A mod B is the remainder of the 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.
Example: items = [4, 10, 10, 6, 2]
The array is not organized since for example, items[2] % 2 = items[3] % 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.
- Assistant