Find the Pivot Integer - Problem
Imagine a perfect balance point where everything to the left equals everything to the right! Given a positive integer n, you need to find a special pivot integer x that creates this magical balance.

The pivot integer x must satisfy this condition: The sum of all integers from 1 to x (inclusive) equals the sum of all integers from x to n (inclusive).

šŸŽÆ Your task: Return the pivot integer if it exists, otherwise return -1. The problem guarantees at most one pivot point exists.

Example: For n = 8, the pivot is 6 because:
• Left sum: 1 + 2 + 3 + 4 + 5 + 6 = 21
• Right sum: 6 + 7 + 8 = 21

Input & Output

example_1.py — Basic Case
$ Input: n = 8
› Output: 6
šŸ’” Note: For n=8, the pivot is 6 because: Left sum (1+2+3+4+5+6) = 21 equals Right sum (6+7+8) = 21
example_2.py — Single Element
$ Input: n = 1
› Output: 1
šŸ’” Note: For n=1, the only number is 1, and it serves as both left and right sum, so the pivot is 1
example_3.py — No Pivot Exists
$ Input: n = 4
› Output: -1
šŸ’” Note: For n=4, no pivot exists. Testing all positions: x=1(1≠10), x=2(3≠9), x=3(6≠7), x=4(10≠4)

Visualization

Tap to expand
The Perfect Seesaw BalancePivot: 612345678Left SideRight SideLeft Weight1+2+3+4+5+6= 21Right Weight6+7+8= 21āš–ļø Perfect Balance Achieved!
Understanding the Visualization
1
Place the blocks
Arrange blocks numbered 1 through n on a number line
2
Test balance points
Try different pivot positions to find perfect balance
3
Calculate weights
Sum the left side (1 to x) and right side (x to n)
4
Find equilibrium
The pivot where left weight equals right weight is our answer
Key Takeaway
šŸŽÆ Key Insight: The pivot x satisfies the equation x² = n(n+1)/2, allowing us to solve directly with mathematics!

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

For each of the n possible pivots, we calculate sums that take O(n) time

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Only using a few variables to store sums and the current pivot

n
2n
āœ“ Linear Space

Constraints

  • 1 ≤ n ≤ 1000
  • The input is always a positive integer
  • At most one pivot integer exists for any given input
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen