2485. Find the Pivot Integer

Updated: 2024-03-13
1 min read
[]

On This Page

LeetCode problem 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