00:00

Problem Statement

Input/Output Examples

Visual Explanation

One pass computes the total; a second pass keeps a running left sum. Every right sum is then derived in constant time instead of being added up again.

Approaches

The running-sum approach is linear. The brute force recomputes both sides for every index.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of elements. The running-sum version makes two passes; the brute force adds up to n values for each of n indexes.

Interview Tips and Recommended Approach

  • Exclude the current element. It belongs to neither side.
  • Treat an empty side as 0. That is why [0] has index 0.
  • Use long sums for large inputs.
  • Know the prefix-sum idea. It turns many range-sum questions into a subtraction.

Recommended Approach: Total and Running Left Sum

Why: it needs one total and one running sum, giving a linear-time answer with constant memory.