Minimum Moves to Obtain Array
Updated
Problem statement
You begin with an array filled with N zeros and you want to obtain an array A.
In one move, you can choose an arbitrary interval and increase all the elements within that interval by 1. For example, you can transform [0, 0, 0, 0] into [0, 1, 1, 1, 0] in a single move. However, you need three moves to obtain [1, 0, 1, 2, 2]. One possible sequence of moves is: [0, 0, 0, 0, 0] → [0, 0, 1, 1, 1] → [0, 0, 1, 2, 2] → [1, 0, 1, 2, 2], where → denotes a single move.
What is the minimum number of moves needed to obtain A, starting from a zero-filled array?
Function
minimumMovesToObtainArray(A: int[]) → int
Write a function solution.
The function is expected to return an integer, the minimum number of moves needed to transform a zero-filled array into A.
solution has the following parameter:
int[] A: an array of integers
Examples
Example 1
A = [2, 1, 3]return = 4
For example, the following sequence of moves leads to the solution: [0, 0, 0] → [1, 1, 1] → [2, 1, 1] → [2, 1, 2] → [2, 1, 3].
Example 2
A = [2, 2, 0, 0, 1]return = 3
The following sequence of moves leads to the solution: [0, 0, 0, 0, 0] → [1, 1, 0, 0, 0] → [2, 2, 0, 0, 0] → [2, 2, 0, 0, 1].
More Google interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Server InvestmentRippling · OARippling interview questions
- Maximum Apples That Fit in a BoxVirtu Financial · OAVirtu Financial interview questions
- Minimum Project Bid CostNVIDIA · OANVIDIA interview questions
- Minimum AmountBelvedere Trading · OA
- Maximum Income from Asset TradingJefferies · OA
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
- Economical TripAccenture · OAAccenture interview questions
- Best Time to Buy and Sell StockFlexTrade · OAFlexTrade interview questions
- Minimum Pile Reduction StepsAmerican Express · OAAmerican Express interview questions