713135. Minimize the sum

Easy

Minimize the sum

Summary

The problem is about minimizing the sum of a given array. It involves sorting and manipulating elements in the array to achieve the minimum sum. The key concepts involved are arrays, sorting, and priority queues.

Detailed Explanation

The problem can be solved by first sorting the array in ascending order. Then, we iterate through the array from left to right, adding each element to a running total. To minimize the sum, we stop when we reach an element that is greater than or equal to the current sum plus 1. This ensures that all smaller elements are added to the sum, minimizing it as much as possible.

Here's a step-by-step breakdown of the solution:

  1. Sort the array in ascending order.
  2. Initialize a variable sum to 0.
  3. Iterate through the sorted array from left to right.
  4. For each element, add it to sum.
  5. If an element is greater than or equal to sum + 1, stop iterating and return sum.

Time complexity: O(n log n) due to sorting Space complexity: O(1)

Here's an ASCII art diagram showing the process:

Initial array: [3, 4, 2, 5]
Sorted array: [2, 3, 4, 5]

Sum = 0
Iterate through array:
- Add 2 to sum (sum = 2)
- Add 3 to sum (sum = 5)
- Stop iterating because next element is 5 > sum + 1
Return sum = 5

Optimized Solutions

Java

View on GeeksforGeeks
public int minimizeSum(int[] arr) {
    Arrays.sort(arr);
    int sum = 0;
    for (int num : arr) {
        sum += num;
        if (num >= sum + 1) {
            break;
        }
    }
    return sum;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.