Skip to main content
All Questions

Find the minimum total added to make an array sorted by adding values to suffixes.

Unlock detailed company stats for this questionUpgrade

Given an array, make it sorted in non-decreasing order using operations that add a value to a suffix of the array, and return the minimum total amount added. Example: [3,4,1,6,2] becomes [3,4,4,9,5] after adding 3 to the suffix from index 2, then [3,4,4,9,9] after adding 4 to the last index, so the answer is 7. Example 2: [3,2,1] gives 2 (it becomes 3,3,2 then 3,3,3 after adding 1 twice).

Brute force uses two loops; the optimal approach traverses once and adds each drop (previous minus current) to a counter.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.