Find an integer x such that the sum of numbers from 1 to x equals the sum of numbers from x to n.
Given a positive integer n, find the pivot integer x such that:
If such an integer exists, return it. Otherwise, return -1.
A pivot integer is allowed to be any integer in the range [1, n].
n.You may assume n >= 1.
x if one exists.-1.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
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.