Find the Pivot Integer (LeetCode 2485): Solution and Explanation

Easy
Math
Prefix sum

Given n, find the integer x between 1 and n where the sum 1 + 2 + ... + x equals the sum x + (x+1) + ... + n. Both sums include x. Return x, or -1 when no such x exists.

Intuition

The direct approach tries every x from 1 to n and compares the two sums. With prefix sums each comparison is O(1), so the whole scan is O(n), which is plenty fast for n up to 1000. But the sums here have closed forms, which leads to something better.

Let total = n(n+1)/2, the sum of the whole range. The left side is the triangle number x(x+1)/2. The right side is everything except the numbers below x, which is total - (x-1)x/2.

Set them equal: x(x+1)/2 = total - x(x-1)/2. Moving the right triangle to the left gives (x² + x + x² - x) / 2 = total, which is simply x² = total. The pivot exists exactly when the total is a perfect square, and then it is the square root.

There is also a nice picture behind it. As x moves right the left side gets heavier and the right side lighter, so the difference changes monotonically and can cross zero at most once. That is why the answer is unique when it exists.

Approach: Perfect square check

  1. Compute total = n * (n + 1) / 2.
  2. Take the integer square root x = floor(sqrt(total)).
  3. If x * x == total, return x.
  4. Otherwise no integer balances the two sides, so return -1.

Step-by-step example

n = 8
  1. total = 8 · 9 / 2 = 36.
  2. x = floor(sqrt(36)) = 6.
  3. 6 · 6 = 36 equals total, so 6 is the pivot.
  4. Check: 1 + ... + 6 = 21 and 6 + 7 + 8 = 21. Return 6.

Code: Python, Java, C++

1class Solution:
2    def pivotInteger(self, n: int) -> int:
3        total = n * (n + 1) // 2
4        x = math.isqrt(total)
5        if x * x == total:
6            return x
7        return -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Perfect square check O(1)
Binary search on x O(log n)
Linear scan with running sums O(n)
Time

O(1)

A multiplication, one square root and one comparison, independent of n.

Space

O(1)

Only two integers are stored.

Other approaches

Linear scan with running sums (time O(n), space O(1))

Keep left as the sum up to x and right as the sum from x to n. Start with x = 1, and each step add x + 1 to the left and remove x from the right. Return x when they match.

Binary search on x (time O(log n), space O(1))

Because left minus right grows as x grows, binary search for the x where the difference is zero. It is a good fallback if you do not spot the algebra.

Common mistakes

  • Forgetting that x belongs to both sums. Excluding it from one side gives a different equation and wrong answers.
  • Trusting a floating point square root without checking x * x == total. Rounding the root and squaring it back is what proves the total is a perfect square.
  • Recomputing both sums from scratch for every candidate x, which turns a simple scan into O(n²) work.
  • Computing total with integer overflow in other variants of the problem with a larger n. Here n is at most 1000, so int is safe.

Frequently asked questions

Why does the pivot satisfy x² = total?

The left sum is x(x+1)/2 and the right sum is total minus (x-1)x/2. Setting them equal and adding the two triangle numbers gives x² on one side and total on the other.

Can there be two pivots?

No. The square root of a number is unique among positive integers, and the left-minus-right difference grows strictly as x increases, so it crosses zero at most once.

Which values of n have a pivot?

Those where n(n+1)/2 is a perfect square, which are rare: 1, 8, 49, 288 and then 1681 beyond the limit. For every other n the answer is -1.