title | seoTitle | description | toc | tags | categories | date | lastMod | featuredImage | weight | ||
---|---|---|---|---|---|---|---|---|---|---|---|
2485. Find the Pivot Integer |
LeetCode 2485. Find the Pivot Integer | Python solution and explanation |
2485. Find the Pivot Integer |
true |
|
2024-01-01 |
2024-01-01 |
2485 |
class Solution:
def pivotInteger(self, n: int) -> int:
if n <= 1:
return n
ar = [1] * n
for i in range(1, n):
ar[i] = ar[i - 1] + i + 1
pivot = 1
while pivot < n:
left = ar[pivot]
right = ar[n - 1] - ar[pivot - 1]
if left == right:
return pivot + 1
pivot += 1
return -1
class Solution:
def pivotInteger(self, n: int) -> int:
y = n * (n + 1) // 2
x = int(sqrt(y))
return x if x * x == y else -1
class Solution:
def pivotInteger(self, n: int) -> int:
for x in range(1, n + 1):
if (1 + x) * x == (x + n) * (n - x + 1):
return x
return -1