LeetCode solutions

2571. Find the Pivot Integer

My accepted Python solution to LeetCode problem 2571, Find the Pivot Integer, running in 7ms.

  • Difficulty: Easy
  • Python
  • Runtime 7ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 7ms, memory 17.5MB, accepted 2026-01-01.

python
class Solution:
    def pivotInteger(self, n: int) -> int:
        total = n * (n + 1) // 2
        left_sum = 0
        for x in range(1, n + 1):
            left_sum += x
            right_sum = total - left_sum + x
            if left_sum == right_sum:
                return x
        return -1

Source