Find the Pivot Integer

Given a positive integer n, find the pivot integer x such that the sum of all numbers from 1 to x inclusive equals the sum of all numbers from x to n inclusive. Return x, or -1 if no such integer exists. For any input there is at most one pivot.

30:00

Find the Pivot Integer
easy
Free written solution
Topics
Companies

Given a positive integer n, find the pivot integer x such that the sum of all numbers from 1 to x inclusive equals the sum of all numbers from x to n inclusive. Return x, or -1 if no such integer exists. For any input there is at most one pivot.

Example 1:
Input: {"n":8}
Output: 6
Constraints:
  • 1≤n≤10001 \leq n \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":8}

Find x in 1..8 where 1 + ... + x equals x + ... + 8; both sides include x itself.

n 8
total ?
x = ⌊√total⌋ ?

staircase 1, 2, …, 8

1

2

3

4

5

6

7

8

Variables
VariableValue
n8
DepthFunction Call
Stack empty
0/5