Skip to main content

Find the Pivot Integer - Solution & Explanation

EasyMathPrefix Sum18 min readAsked at: Amazon, Meta, Google
Practice this problem

Problem Statement

Given a positive integer n, find the pivot integer x such that:

  • The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively.

Return the pivot integer x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input.

 

Example 1:

Input: n = 8
Output: 6
Explanation: 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.

Example 2:

Input: n = 1
Output: 1
Explanation: 1 is the pivot integer since: 1 = 1.

Example 3:

Input: n = 4
Output: -1
Explanation: It can be proved that no such integer exist.

 

Constraints:

  • 1 <= n <= 1000

Approach Overview

Problem Overview: You are given an integer n. The task is to find an integer x such that the sum of numbers from 1 to x equals the sum of numbers from x to n. If such a number exists, return it. Otherwise return -1.

Approach 1: Brute Force Search (O(n^2) time, O(1) space)

The direct method tries every candidate pivot from 1 to n. For each candidate x, compute the left sum by iterating from 1 to x and the right sum by iterating from x to n. If both sums match, that x is the pivot integer. This approach mirrors the definition of the problem and is useful when first reasoning about the condition. However, each candidate requires two loops, which results in O(n^2) time in the worst case while using constant memory.

The logic here relates closely to the idea of cumulative sums often discussed in Prefix Sum problems. Instead of storing prefix values, the brute force version recomputes them repeatedly.

Approach 2: Optimized Arithmetic Approach (O(1) time, O(1) space)

The key observation is that the sum of the first k integers follows the formula k(k+1)/2. Let S = n(n+1)/2 be the total sum from 1 to n. If x is the pivot, then the sum from 1 to x equals the sum from x to n. Writing this mathematically:

1 + 2 + ... + x = x + (x+1) + ... + n

After simplifying with arithmetic series formulas, the equation becomes x^2 = S. That means the pivot exists only when the total sum is a perfect square. Compute S, take its square root, and check whether it is an integer. If it is, that integer is the pivot. Otherwise no pivot exists.

This turns what initially looks like a cumulative sum search into a simple math check using properties of arithmetic series. The computation runs in constant time and uses constant memory, making it the most efficient solution. The reasoning connects to patterns commonly seen in Math problems where recognizing a formula removes the need for iteration.

Recommended for interviews: Start by explaining the brute force idea since it directly follows the definition of the pivot condition. Then derive the arithmetic relationship and show how the equality simplifies to x^2 = n(n+1)/2. Interviewers expect the optimized mathematical insight because it demonstrates pattern recognition and the ability to convert a Prefix Sum style condition into a constant‑time formula.

Approach 1: Brute Force Search

This approach involves iterating through each number from 1 to n and checking if it satisfies the given condition of a pivot integer. For each candidate pivot integer x, calculate the sum of numbers from 1 to x using the formula for the sum of an arithmetic series. Similarly, calculate the sum from x to n. Compare these sums to determine if x is a pivot integer.

The function find_pivot iterates through numbers from 1 to n. For each number, it calculates the sum of numbers from 1 to x (sum1) and from x to n (sum2). It returns x if the two sums are equal, otherwise returns -1 at the end.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n).
Space Complexity: O(1).

Try this approach in the editor →

Approach 2: Optimized Arithmetic Approach

Instead of iterating through each possible x, we can use mathematical formulation. As the sum from 1 to n is fixed, let's denote the total sum as S. Then, for pivot x, the condition becomes:

(x * (x + 1)) / 2 = (S - ((x - 1) * x) / 2)

From this, derive a formula to find x directly if it exists. Solving algebraically can help us identify the pivot integer without iteration.

This optimized solution uses algebra to directly verify if a number x is a pivot without recalculating sums within the loop.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n).
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Enumeration

We can directly enumerate x in the range of [1,..n], and check whether the following equation holds. If it holds, then x is the pivot integer, and we can directly return x.

$ (1 + x) times x = (x + n) times (n - x + 1)

The time complexity is O(n), where n is the given positive integer n. The space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

Rust

PHP

Try this approach in the editor →

Approach 4: Mathematics

We can transform the above equation to get:

$ n times (n + 1) = 2 times x^2

That is:

x = \sqrt{\frac{n times (n + 1)}{2}}

If x is an integer, then x is the pivot integer, otherwise there is no pivot integer.

The time complexity is O(1), and the space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Brute Force Search

Time Complexity: O(n).
Space Complexity: O(1).

Optimized Arithmetic Approach

Time Complexity: O(n).
Space Complexity: O(1).

Enumeration
Mathematics

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SearchO(n^2)O(1)When first reasoning about the problem or validating the pivot definition step by step
Optimized Arithmetic ApproachO(1)O(1)Preferred solution for interviews and production due to constant time computation

Video Solution

Find the Pivot Integer | 5 Approaches | Easy Explanation | Leetcode 2485 | codestorywithMIKcodestorywithMIK3,991 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find the Pivot Integer easy or hard?
The problem is rated Easy because the constraints are small and the logic is straightforward. The main challenge is recognizing that the equality of two ranges leads to the equation x² = n(n+1)/2, which immediately gives the pivot in constant time.
Find the Pivot Integer Python/Java solution
Most implementations compute the total sum n(n+1)/2 and check whether its square root is an integer. Python uses math.sqrt or integer checks, while Java typically uses Math.sqrt and verifies that the square of the result equals the sum.
How to solve Find the Pivot Integer in O(1)?
Compute the total sum S = n(n+1)/2 using the arithmetic series formula. If an integer x exists such that x² = S, then x is the pivot integer. Check whether sqrt(S) is an integer and return it; otherwise return -1.
What is the best approach for Find the Pivot Integer?
The optimal approach uses an arithmetic formula derived from the sum of the first n integers. If S = n(n+1)/2 is the total sum, the pivot must satisfy x² = S. Compute S and check whether its square root is an integer. This gives an O(1) time and O(1) space solution.
Is Find the Pivot Integer asked at Google/Amazon/Meta?
The exact problem may appear less frequently, but the underlying pattern—using arithmetic series and prefix sum reasoning—is common in coding interviews at companies like Google, Amazon, and Meta. Interviewers often test whether candidates can derive mathematical shortcuts from cumulative sum conditions.
What data structure is used in Find the Pivot Integer?
No special data structure is required. The problem relies on mathematical properties of arithmetic series and the concept behind prefix sums. The optimized solution uses simple integer arithmetic and a square root check.
What is the time complexity of Find the Pivot Integer?
The brute force method runs in O(n^2) time because it recomputes sums for each candidate pivot. The optimized arithmetic solution runs in O(1) time since it only calculates n(n+1)/2 and checks whether the result is a perfect square.

Ready to solve this problem?

Practice Find the Pivot Integer with our built-in code editor and test cases.

Practice on FleetCode