Skip to content

Commit 1534755

Browse files
solves #2485: Find the Pivot Integer in java
1 parent f84f158 commit 1534755

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@
783783
| 2469 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature) | [![Java](assets/java.png)](src/ConvertTheTemperature.java) | |
784784
| 2475 | [Number of Unequal Triplets in Array](https://leetcode.com/problems/number-of-unequal-triplets-in-array) | [![Java](assets/java.png)](src/NumberOfUnequalTripletsInArray.java) | |
785785
| 2481 | [Minimum Cuts to Divide a Circle](https://leetcode.com/problems/minimum-cuts-to-divide-a-circle) | [![Java](assets/java.png)](src/MinimumCutsToDivideACircle.java) | |
786-
| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer) | | |
786+
| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer) | [![Java](assets/java.png)](src/FindThePivotInteger.java) | |
787787
| 2490 | [Circular Sentence](https://leetcode.com/problems/circular-sentence) | | |
788788
| 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities) | [![Python](assets/python.png)](python/minimum_score_of_a_path_between_two_cities.py) | |
789789
| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array) | | |

src/FindThePivotInteger.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/find-the-pivot-integer
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class FindThePivotInteger {
6+
public int pivotInteger(int n) {
7+
for (int pivot = 1, left = 1, right = summation(n) ; pivot <= n ; pivot++) {
8+
if (left == right) return pivot;
9+
if (left > right) break;
10+
left += pivot + 1;
11+
right -= pivot;
12+
}
13+
return -1;
14+
}
15+
16+
private int summation(int n) {
17+
return (n * (n + 1)) / 2;
18+
}
19+
}

0 commit comments

Comments
 (0)