Skip to main content
Back to problems
Leetcode
Easy
Math
Number Theory
Find The Pivot Integer

Find an integer x such that the sum of numbers from 1 to x equals the sum of numbers from x to n.

Acceptance 0%
Problem Statement

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

  • the sum of all integers from 1 to x is equal to
  • the sum of all integers from x to n

If such an integer exists, return it. Otherwise, return -1.

A pivot integer is allowed to be any integer in the range [1, n].

Input Format

  • A single integer n.

You may assume n >= 1.

Output Format

  • Return the pivot integer x if one exists.
  • Otherwise, return -1.

Constraints

  • 1n1 \le n
  • The answer, if it exists, is an integer in the range [1,n][1, n].
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 8

Output

6

Explanation

Sum from 1 to 6 is 21, and sum from 6 to 8 is also 21. So the pivot integer is 6.

Example 2

Input

n = 4

Output

-1

Explanation

No integer x in [1, 4] makes the sum from 1 to x equal the sum from x to 4.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.