Skip to content

Commit a35c8dc

Browse files
solves #2441: Largest Positive Integer That Exists With Its Negative in java
1 parent 6329d0d commit a35c8dc

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@
773773
| 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors) | [![Java](assets/java.png)](src/NumberOfCommonFactors.java) | |
774774
| 2432 | [The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task) | [![Java](assets/java.png)](src/TheEmployeeThatWorkedOnTheLongestTask.java) | |
775775
| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times) | [![Java](assets/java.png)](src/NumberOfValidClockTimes.java) | |
776-
| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative) | | |
776+
| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative) | [![Java](assets/java.png)](src/LargestPositiveIntegerThatExistsWithItsNegative.java) | |
777777
| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict) | | |
778778
| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference) | | |
779779
| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class LargestPositiveIntegerThatExistsWithItsNegative {
9+
public int findMaxK(int[] array) {
10+
int largest = -1;
11+
final Set<Integer> numbers = new HashSet<>();
12+
13+
for (int element : array) {
14+
if (Math.abs(element) > largest && numbers.contains(-element)) {
15+
largest = Math.abs(element);
16+
}
17+
numbers.add(element);
18+
}
19+
20+
return largest;
21+
}
22+
}

0 commit comments

Comments
 (0)