2485. Find the Pivot Integer

T : O(n)

S: O(1)

```java
class Solution {
    public int pivotInteger(int n) {
        if (n == 1) {
            return 1;
        }
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        //System.out.println(sum);
        int pivotSum = 0;
        for (int i = 1; i <= n; i++) {
            pivotSum += i;
            if (pivotSum == (i+sum)/2 && (i+sum)%2 == 0) {
                return i;
            }
        }
        return -1;
    }
}

/**
   sum 0~i-1 = sum[i~n]
[             ] [   ]


cal total

42/2


1 2 3   34

6.  5+3
 */
```

Last updated