Problem · Array

Calculate Leftover Stacks

Learn this problem
EasyMicrosoft logoMicrosoftOA
See Microsoft hiring insights

Problem statement

Given a list of stacks (just a number), each stack has the following operation: every 2 blocks get converted over to 1 and pushed to the next stack. Your goal is to calculate the number of stacks of 1 leftover by the time you complete all possible stack operations.

Function

calculateLeftoverStacks(stacks: int[]) → int

Examples

Example 1

stacks = [5, 3, 1]return = 4
Following the given operations:
  • 4 from the first stack can get converted into 2 to the next stack with one left over
  • 2 + 3 = 5 which follows the same, one left over with the 4 converted into 2 for the next stack
  • 2 + 1 = 3 which makes two blocks get converted into 1 into the next stack
  • 1 + 0 = 1, no possible operation
Thus, there are 4 stacks with 1 block leftover.

Constraints

🦊🦊

More Microsoft problems

drafts saved locally
public int calculateLeftoverStacks(int[] stacks) {
  // write your code here
}
stacks[5, 3, 1]
expected4
checking account