python-algorithm
leetcode 2485. Find the Pivot Integer
무적김두칠
2023. 1. 10. 10:05
https://leetcode.com/problems/find-the-pivot-integer/description/
Find the Pivot Integer - LeetCode
Find the Pivot Integer - Given a positive integer n, find the pivot integer x such that: * The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively. Return the pivot integer x. If no such integer exists
leetcode.com
1
2
3
4
5
6
7
8
9
10
|
class Solution:
def pivotInteger(self, n: int) -> int:
if n == 1 :
return 1
answer = -1
for i in range(1, n):
if i*(i+1) == int((n)*(n+1)/2+i):
answer = i
return answer
|
cs |
반응형