FastPrepFastPrep
Problem Brief

Minimum Tokens Remaining

OA
See Microsoft online assessment and hiring insights

Let us consider an infinite sequence of stacks indexed from 0, and an exchange operation that removes two tokens from a stack and adds one token to the next stack.

For example, lets assume there are two tokens on stack 0 and three on stack 1. Two tokens from stack 0 may be exchanged for one new token on stack 1. After that operation, there are four tokens on stack 1 that may be exchanged for two new tokens on stack 2. Finally, a new token may be added to stack 3 by exchanging two tokens from stack 2. This gives us: stacks 0, 1 and 2 empty, and stack 3 with one token.

Problem Image

Given the heights of the first N stacks, find the minimum number of tokens that may remain after any number of exchange operations. You may assume that all of the tokens are identical. All uninitialized stacks are empty by default.

Function Description

Write a function:

class Solution { public int solution(int[] A); }

that, given an array A of N integers, representing the heights of the first N stacks in the sequence, returns the minimum number of tokens which may remain on the stacks after any number of exchange operations.

1Example 1

Input
A = [2, 3]
Output
1
Explanation
Given A = [2, 3], the function should return 1, as explained above.
public int minimumTokensRemaining(int[] A) {
  // write your code here
}
Input

A

[2, 3]

Output

1

Sign in to submit your solution.