Skip to content

Commit 109ea89

Browse files
authored
Merge pull request #150 from fartem/find-the-pivot-integer
2023-01-06 update: added "2485. Find the Pivot Integer"
2 parents a799614 + aaf7871 commit 109ea89

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
424424
| 2455. Average Value of Even Numbers That Are Divisible by Three | [Link](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/AverageValueOfEvenNumbersThatAreDivisibleByThree.java) |
425425
| 2469. Convert the Temperature | [Link](https://leetcode.com/problems/convert-the-temperature/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/ConvertTheTemperature.java) |
426426
| 2481. Minimum Cuts to Divide a Circle | [Link](https://leetcode.com/problems/minimum-cuts-to-divide-a-circle/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/MinimumCutsToDivideACircle.java) |
427+
| 2485. Find the Pivot Integer | [Link](https://leetcode.com/problems/find-the-pivot-integer/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/FindThePivotInteger.java) |
427428
| 2490. Circular Sentence | [Link](https://leetcode.com/problems/circular-sentence/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/CircularSentence.java) |
428429
| 2496. Maximum Value of a String in an Array | [Link](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/MaximumValueOfAStringInAnArray.java) |
429430
| 2500. Delete Greatest Value in Each Row | [Link](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/DeleteGreatestValueInEachRow.java) |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
// https://leetcode.com/problems/find-the-pivot-integer/
4+
public class FindThePivotInteger {
5+
6+
private final int input;
7+
8+
public FindThePivotInteger(int input) {
9+
this.input = input;
10+
}
11+
12+
public int solution() {
13+
int sum = input * (input + 1) / 2;
14+
int value = 0;
15+
for (int i = input; i > 0; i--) {
16+
value += i;
17+
if (sum == value) {
18+
return i;
19+
}
20+
sum -= i;
21+
}
22+
return -1;
23+
}
24+
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class FindThePivotIntegerTest {
8+
9+
@Test
10+
public void defaultTests() {
11+
assertEquals(6, new FindThePivotInteger(8).solution());
12+
assertEquals(1, new FindThePivotInteger(1).solution());
13+
assertEquals(-1, new FindThePivotInteger(4).solution());
14+
}
15+
16+
}

0 commit comments

Comments
 (0)