Equilibrium Subarray Split Queries — Problem Statement & Solution Guide

ArraysMediumrange-sum-query-using-prefix-sum
TimeO(N + Q·log N)
|
SpaceO(N)

Quick Answer & Algorithm Key Takeaway

Master coding challenges related to Arrays and solve the Equilibrium Subarray Split Queries problem optimally.

TopicArrays
Patternrange-sum-query-using-prefix-sum
TimeO(N + Q·log N)
SpaceO(N)

Problem Description

Given an array of positive integers nums of size N, and a 2D array queries of size Q, where each query is represented as a pair of indices [L, R] (0-based). For each query, you need to determine if there exists a partition index M (where L <= M < R) such that the sum of the elements from index L to M is exactly equal to the sum of the elements from index M + 1 to R. Return an array of bool values representing whether each query has an equilibrium subarray split.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Equilibrium Subarray Split Queries"

medium

WHY DOES IT MATTER?

Efficient range‑split detection is a classic example of reducing per‑query work from linear to logarithmic.

OPTIMIZATION CHALLENGE

The key is turning a linear scan into a binary search by pre‑indexing prefix sums.

REAL-WORLD CONNECTION

Similar techniques power load‑balancing systems that need to split workloads evenly across servers.

Cache the prefix‑sum array and reuse a single unordered_map to avoid rebuilding structures for each query.

COMPLEXITY AT A GLANCE

⏱ Time:O(N + Q·log N)
💾 Space:O(N)

Core Theory — Why This Approach?

The problem reduces to finding a split point where the left and right sub‑array sums are equal. Using prefix sums, the sum of any subarray [L, R] is prefix[R+1]‑prefix[L]; the condition becomes prefix[M+1]‑prefix[L] = (prefix[R+1]‑prefix[L]) / 2, which is a simple arithmetic check for a target prefix value. A naïve scan for each query would be O(N) per query, leading to O(N·Q) time that fails for N, Q up to 10^5. By pre‑computing prefix sums and indexing each distinct prefix value with a sorted list of its positions, we can answer each query with a binary search for the target value, achieving O(log N) per query and O(N) total preprocessing, which is optimal for large inputs.

Interview Questions on This Problem

Q1How does prefix‑sum transformation simplify range‑sum queries?

Prefix sums turn any range sum into a constant‑time subtraction of two stored values. This eliminates the need to iterate over the range for each query.

Q2Why is a hash map of prefix values to index lists preferable to a plain hash set here?

A set only tells you if a value exists, but we need to ensure the split index lies within the query bounds. Storing sorted index lists lets us binary‑search for a position inside [L+1, R].

Q3What is the impact of all numbers being positive on the algorithm?

Positive numbers guarantee that prefix sums are strictly increasing, simplifying the existence check for the target value. It also ensures the total sum cannot be zero unless the subarray is empty.

Examples

Example 1

Input

[1, 2, 3, 4, 5], [[0, 4]]

Output

[true]

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and query [0, 4], we can find a partition index M = 2 where the sum of elements from index 0 to 2 is 1 + 2 + 3 = 6 and the sum of elements from index 3 to 4 is 4 + 5 = 9. However, there is no such partition index, but we can see that for M = 1, the sum of elements from index 0 to 1 is 1 + 2 = 3 and the sum of elements from index 2 to 4 is 3 + 4 + 5 = 12. Still, no equilibrium point. But for M = 3, the sum of elements from index 0 to 3 is 1 + 2 + 3 + 4 = 10 and the sum of elements from index 4 to 4 is 5. No equilibrium point. However, if we consider the query [0, 2], we can see that for M = 1, the sum of elements from index 0 to 1 is 1 + 2 = 3 and the sum of elements from index 2 to 2 is 3. Thus, for the query [0, 2], there exists an equilibrium point.

Example 2

Input

[10, 10], [[0, 1]]

Output

[true]

Explanation: Step-by-step: with input [10, 10] and query [0, 1], we can find a partition index M = 0 where the sum of elements from index 0 to 0 is 10 and the sum of elements from index 1 to 1 is 10. Thus, there exists an equilibrium point.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^4
  • 1 <= queries.length <= 10^5
  • queries[i].length == 2
  • 0 <= L <= R < nums.length

Optimal Approach & Strategy

Build prefix sums and a map from each sum to its sorted indices; for each query binary‑search for the target half‑sum within the index range.

Brute Force Approach

Iterate M from L to R‑1, compute left and right sums each time, and compare – O(N) per query.

Code Solutions

JavaScript Solution
Time: O(N + Q·log N)
function equilibriumSubarraySplitQueries(nums, queries) {
       const result = [];
       for (let query of queries) {
           let found = false;
           for (let m = query[0]; m < query[1]; m++) {
               let sumLeft = 0;
               let sumRight = 0;
               for (let i = query[0]; i <= m; i++) {
                   sumLeft += nums[i];
               }
               for (let i = m + 1; i <= query[1]; i++) {
                   sumRight += nums[i];
               }
               if (sumLeft === sumRight) {
                   found = true;
                   break;
               }
           }
           result.push(found);
       }
       return result;
   }

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.