Find the Pivot Integer

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
Inputn = 8
Output6
6 is the pivot integer since 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.
Example 2
Inputn = 1
Output1
1 is the pivot integer since 1 = 1.

Constraints

  • 1 <= n <= 1000

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate