Skip to main content
All Questions

Find the minimum cost to make all array elements equal with prefix and suffix overwrites.

Unlock detailed company stats for this questionUpgrade

You are given an array of length n and can perform these operations:

  1. Select an index i such that 1 <= i <= n - 1, and set all elements from index 0 to i - 1 to arr[i]; the cost is i * arr[i].
  2. Select an index i such that 0 <= i <= n - 2, and set all elements from index i + 1 to n - 1 to arr[i]; the cost is (n - 1 - i) * arr[i]. Return the minimum cost to make all elements of the array the same.

Constraints: 1 <= N <= 10^3, -10^5 <= arr[i] <= 10^5.

Example: input [1, 1, 2, 1, 1], output 3. Selecting index 1 and overwriting the suffix costs (5 - 1 - 1) * 1 = 3, giving [1, 1, 1, 1, 1].

Format · Online assessment.

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.