2485. Find the Pivot Integer
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 84% Topics: Math, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(1)
# Space: O(1)
# math
class Solution(object):
def pivotInteger(self, n):
"""
:type n: int
:rtype: int
"""
x = int(((n+1)*n//2)**0.5+0.5)
return x if x**2 == (n+1)*n//2 else -1
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions