Make Array Bitonic
Updated
Problem statement
You are given an array arr of n integers. In one operation you can lower any element by 1. Return the minimum number of operations needed to make arr bitonic.
In this problem, a bitonic array may start and end with any number of zeros. The non-zero part in the middle must go up by 1 at a time from 1 to some peak k, then down by 1 at a time back to 1. For example, [0, 1, 2, 3, 2, 1, 0, 0] is bitonic.
Function
minOperationsToMakeBitonic(arr: int[]) → intExamples
Example 1
arr = [3,3,3,3,3]return = 6Lower the array to [1, 2, 3, 2, 1]. That takes 2 + 1 + 0 + 1 + 2 = 6 operations, the fewest possible.
Example 2
arr = [1,1,3,1,1]return = 3Lower the array to [0, 1, 2, 1, 0]. That takes 1 + 0 + 1 + 0 + 1 = 3 operations. A peak of 3 in the middle would need [1, 2, 3, 2, 1], but the second element is only 1 and can never go up.
Example 3
arr = [1,2,1,3,2]return = 5Lower the array to [1, 2, 1, 0, 0] (or to [0, 0, 1, 2, 1]). Either way 4 of the total 9 stays, so 9 - 4 = 5 operations.
Constraints
1 ≤ n ≤ 10^5, wherenis the length ofarr1 ≤ arr[i] ≤ 10^4
More Amazon interview questions
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- Count Connected ComponentsPHONE SCREEN · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge Accounts by Shared AttributePHONE SCREEN · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Minimum Grid InconvenienceOA · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Flood FillONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- At Most K Jumps with Quadratic Transition CostInfosys · OAInfosys interview questions
- Largest Consistent Visiting GroupAuxoAI · OA
- Delete K Subsets for Maximum ScoreRubrik · OARubrik interview questions
- Optimize Discount Selection Under a Point BudgetPalantir · OAPalantir interview questions
- Maximum Index-Weighted Sum with Disjoint SwapsVisa · OAVisa interview questions
- Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions
- Pressure-Isolation Shutdown OrderIntuit · OAIntuit interview questions
- Get Minimum IncrementIBM · OAIBM interview questions
- Jump Game with Prime-3 StepsUber · OAUber interview questions
- Maximum Payout with Context SwitchesPhonePe · OAPhonePe interview questions