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.
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.
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.
A = [2, 3] return = 1
A = [2, 3], the function should return 1, as explained above.- Rank Open BusinessesPHONE SCREEN · Seen May 2026
- Retain Top K ValuesPHONE SCREEN · Seen May 2026
- In-Memory SQL with CSV InitializationONSITE INTERVIEW · Seen May 2026
- Order Records by Matching Start and EndONSITE INTERVIEW · Seen May 2026
- Recover Corrupted Master PageONSITE INTERVIEW · Seen Feb 2026
- Distinct Number Line MovesOA · Seen Oct 2025
- Minimum Round Trip LengthsOA · Seen Aug 2025
- Programmer StringsOA · Seen Aug 2025
public int minimumTokensRemaining(int[] A) {
// write your code here
}