Skip to content

Commit 7ba1720

Browse files
Closest number to zero (#16651)
* added implementation and test Signed-off-by: Emmanuel Mireku Omari <[email protected]> * tests cleaned Signed-off-by: Emmanuel Mireku Omari <[email protected]> * formatted test Signed-off-by: Emmanuel Mireku Omari <[email protected]> * added parenthesis to the Bruteforce conditions Signed-off-by: Emmanuel Mireku Omari <[email protected]> * removed unused tests Signed-off-by: Emmanuel Mireku Omari <[email protected]> --------- Signed-off-by: Emmanuel Mireku Omari <[email protected]> Signed-off-by: Emmanuel Mireku Omari <[email protected]> Co-authored-by: Emmanuel Mireku Omari <[email protected]>
1 parent 9597806 commit 7ba1720

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.closesttozero;
2+
3+
public class BruteForce {
4+
5+
public static int findClosestToZero(int[] arr) throws IllegalAccessException {
6+
if (arr == null || arr.length == 0) {
7+
throw new IllegalAccessException("Array must not be null or Empty");
8+
}
9+
10+
int closest = arr[0];
11+
for (int i = 1; i < arr.length; i++) {
12+
if ((Math.abs(arr[i]) < Math.abs(closest)) || ((Math.abs(arr[i]) == Math.abs(closest)) && (arr[i] > closest))) {
13+
closest = arr[i];
14+
}
15+
}
16+
return closest;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.closesttozero;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class PriorityQueueToZero {
6+
public static int findClosestToZeroWithPriorityQueue(int[] arr, int k) {
7+
if (arr == null || arr.length == 0 || k <= 0) {
8+
throw new IllegalArgumentException("Invalid input");
9+
}
10+
11+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> Math.abs(b) - Math.abs(a));
12+
13+
for (int num : arr) {
14+
pq.offer(num);
15+
if (pq.size() > k) {
16+
pq.poll();
17+
}
18+
}
19+
20+
return pq.peek();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.closesttozero;
2+
3+
import java.util.Arrays;
4+
5+
public class SortingAndBinarySearch {
6+
7+
public static int findClosestToZero(int[] arr) {
8+
if (arr == null || arr.length == 0) {
9+
throw new IllegalArgumentException("Array must not be null or Empty");
10+
}
11+
12+
Arrays.sort(arr);
13+
int closestNumber = arr[0];
14+
int left = 0;
15+
int right = arr.length - 1;
16+
17+
while (left <= right) {
18+
int mid = left + (right - left) / 2;
19+
20+
if (Math.abs(arr[mid]) < Math.abs(closestNumber)) {
21+
closestNumber = arr[mid];
22+
}
23+
24+
if (arr[mid] < 0) {
25+
left = mid + 1;
26+
} else if (arr[mid] > 0) {
27+
right = mid - 1;
28+
} else {
29+
return arr[mid];
30+
}
31+
}
32+
33+
return closestNumber;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.closesttozero;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Random;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
class BruteForceUnitTest {
11+
12+
@Test
13+
void whenFindingClosestToZeroWithBruteForce_thenResultShouldBeCorrect() throws IllegalAccessException {
14+
int[] arr = {11, 60, -1, 70, -11, 85};
15+
assertEquals(-1, BruteForce.findClosestToZero(arr));
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.closesttozero;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Random;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
class PriorityQueueTOZeroUnitTest {
11+
@Test
12+
void whenFindingClosestToZeroWithBruteForce_thenResultShouldBeCorrect() throws IllegalAccessException {
13+
int[] arr = {1, 60, -10, 70, -80, 85};
14+
assertEquals(1, PriorityQueueToZero.findClosestToZeroWithPriorityQueue(arr, 1));
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.closesttozero;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
class SortingAndBinarySearchUnitTest {
8+
9+
@Test
10+
void whenFindingClosestToZeroWithBruteForce_thenResultShouldBeCorrect() throws IllegalAccessException {
11+
int[] arr = {1, 60, -10, 70, -80, 85};
12+
assertEquals(1, SortingAndBinarySearch.findClosestToZero(arr));
13+
}
14+
15+
}

0 commit comments

Comments
 (0)