702737. Balance with respect to an array

Easy

Balance with respect to an array

Summary

The problem is about finding the balance point in a given array. The balance point is defined as the index at which the sum of elements on the left side equals the sum of elements on the right side, considering all elements up to that point.

Detailed Explanation

To solve this problem, we can use a two-pointer approach. Initialize two pointers, left and right, at the beginning and end of the array, respectively. Calculate the sum of elements from the start to left (inclusive) and from right to the end (exclusive).

Move the pointer that has the smaller sum towards the other. This is because we want to minimize the distance between the two sums.

When the two pointers meet, it means we have found the balance point. If the array is already balanced, the pointers will not move.

Here's a step-by-step breakdown:

  1. Initialize left and right at 0 and n-1, respectively.
  2. Calculate sum_left as the sum of elements from start to left.
  3. Calculate sum_right as the sum of elements from right to end (exclusive).
  4. If sum_left <= sum_right, move left towards right. Otherwise, move right towards left.
  5. Repeat steps 2-4 until left and right meet.
  6. The meeting point is the balance point.

Time complexity: O(n), where n is the length of the array. We only need to traverse the array once.

Space complexity: O(1). We don't use any extra space that scales with the input size.

Optimized Solutions

Java

View on GeeksforGeeks
public int findBalance(int[] arr) {
    int left = 0, right = arr.length - 1;
    while (left <= right) {
        int sum_left = 0, sum_right = 0;
        for (int i = 0; i <= left; i++) {
            sum_left += arr[i];
        }
        for (int i = right; i < arr.length; i++) {
            sum_right += arr[i];
        }
        if (sum_left <= sum_right) {
            left++;
        } else {
            right--;
        }
    }
    return left;
}

DevExCode

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

© 2026 DevExCode. All rights reserved.