Skip to content

Commit a03755f

Browse files
committed
Problem 4.6 TADM
1 parent 79f52eb commit a03755f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.shekhargulati.tadm.ch04.excercises;
2+
3+
import java.util.Arrays;
4+
5+
public class Problem4_6 {
6+
7+
public static boolean findPair(int[] s1, int[] s2, int x) {
8+
/*
9+
Sort one array let say s2
10+
for each el in s1
11+
do a binary search in s2 for x-el
12+
if found return el and (x-el)
13+
*/
14+
Arrays.sort(s2);
15+
for (int f : s1) {
16+
int s = x - f;
17+
if (Arrays.binarySearch(s2, s) >= 0) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.shekhargulati.tadm.ch04.excercises;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class Problem4_6Test {
8+
9+
@Test
10+
public void shouldFindAPair() throws Exception {
11+
boolean pair = Problem4_6.findPair(new int[]{4, 3, 7}, new int[]{3, 2, 1}, 5);
12+
assertTrue(pair);
13+
}
14+
}

0 commit comments

Comments
 (0)