Find all pairs in an array that sum to a given number without using HashMap

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Find all pairs in an array that sum to a given number without using
HashMap.
Duplicate pairs are not allowed. Input array cannot be modified.
input:
-2, -1, -1, 5, 7, 7, 7, 7, 8, target =7
output:
(-1, 8)
As it is mentioned, using HashMap is not allowed, so I decided to use binary search.
GitHub
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllParis(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = input.length - 1; low < high; )
int sum = input[low] + input[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
@Test
public void findAllParis() throws Exception
System.out.println(TwoSumProblemUsingBinarySearch.findAllParis(new int-2, -1, -1, 5, 7, 7, 7, 7, 8, 7));
assertEquals(1, TwoSumProblemUsingBinarySearch.findAllParis(new int-2, -1, -1, 5, 7, 7, 7, 7, 8, 7).size());
java algorithm programming-challenge interview-questions k-sum
add a comment |Â
up vote
1
down vote
favorite
Find all pairs in an array that sum to a given number without using
HashMap.
Duplicate pairs are not allowed. Input array cannot be modified.
input:
-2, -1, -1, 5, 7, 7, 7, 7, 8, target =7
output:
(-1, 8)
As it is mentioned, using HashMap is not allowed, so I decided to use binary search.
GitHub
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllParis(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = input.length - 1; low < high; )
int sum = input[low] + input[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
@Test
public void findAllParis() throws Exception
System.out.println(TwoSumProblemUsingBinarySearch.findAllParis(new int-2, -1, -1, 5, 7, 7, 7, 7, 8, 7));
assertEquals(1, TwoSumProblemUsingBinarySearch.findAllParis(new int-2, -1, -1, 5, 7, 7, 7, 7, 8, 7).size());
java algorithm programming-challenge interview-questions k-sum
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Find all pairs in an array that sum to a given number without using
HashMap.
Duplicate pairs are not allowed. Input array cannot be modified.
input:
-2, -1, -1, 5, 7, 7, 7, 7, 8, target =7
output:
(-1, 8)
As it is mentioned, using HashMap is not allowed, so I decided to use binary search.
GitHub
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllParis(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = input.length - 1; low < high; )
int sum = input[low] + input[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
@Test
public void findAllParis() throws Exception
System.out.println(TwoSumProblemUsingBinarySearch.findAllParis(new int-2, -1, -1, 5, 7, 7, 7, 7, 8, 7));
assertEquals(1, TwoSumProblemUsingBinarySearch.findAllParis(new int-2, -1, -1, 5, 7, 7, 7, 7, 8, 7).size());
java algorithm programming-challenge interview-questions k-sum
Find all pairs in an array that sum to a given number without using
HashMap.
Duplicate pairs are not allowed. Input array cannot be modified.
input:
-2, -1, -1, 5, 7, 7, 7, 7, 8, target =7
output:
(-1, 8)
As it is mentioned, using HashMap is not allowed, so I decided to use binary search.
GitHub
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllParis(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = input.length - 1; low < high; )
int sum = input[low] + input[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
@Test
public void findAllParis() throws Exception
System.out.println(TwoSumProblemUsingBinarySearch.findAllParis(new int-2, -1, -1, 5, 7, 7, 7, 7, 8, 7));
assertEquals(1, TwoSumProblemUsingBinarySearch.findAllParis(new int-2, -1, -1, 5, 7, 7, 7, 7, 8, 7).size());
java algorithm programming-challenge interview-questions k-sum
edited Aug 2 at 4:48
200_success
123k14142399
123k14142399
asked Mar 24 at 18:30
Exploring
20512
20512
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
3
down vote
- Typo in method name as pointed out by @Stingy. Should be
findAllPairs. - Accessing wrong array: You are reading values from
inputinstead of your sorted copynumbers. This is probably a typo. E.g.int sum = input[low] + input[high];should beint sum = numbers[low] + numbers[high]; Your
forloop has an empty update statement, this isn't wrong per-se but a bit unusual. Consider an alternative usingwhileinstead:int low = 0, high = numbers.length-1;
while (low < high)
int sum = numbers[low] + numbers[high];
//...- Insufficient test: As evidenced by its failure to catch the bug in 2. the test is too limited to be of much help. Consider checking more than one example (especially corner-cases like an empty input array) and checking against the expected output instead of just its size.
ooooo...indeed typo it should benumbers:-)
â Exploring
Mar 25 at 13:23
add a comment |Â
up vote
1
down vote
You can prevent duplicate pairs by incrementing/decrementing your counters not only once, but until they point to a number that is greater/lesser than the number they previously pointed to. That way, you can store the pairs in a List instead of a Set, which will probably be faster, because the List doesn't need to check whether it already contains a pair that is equal to the one being added.
Also, there is a typo in your method names findAllParis.
add a comment |Â
up vote
0
down vote
accepted
Incorporating the feedbacks, here is the answer
Github: https://github.com/Ramblers-Code/CodeKata/blob/master/src/main/java/kata/array/TwoSumProblemUsingBinarySearch.java#18
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllPairs(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = numbers.length - 1; low < high; )
int sum = numbers[low] + numbers[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
add a comment |Â
up vote
-2
down vote
You would also have to add this to the findAllPairs method to sort the initial list which is the input:
Arrays.sort(input);
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
- Typo in method name as pointed out by @Stingy. Should be
findAllPairs. - Accessing wrong array: You are reading values from
inputinstead of your sorted copynumbers. This is probably a typo. E.g.int sum = input[low] + input[high];should beint sum = numbers[low] + numbers[high]; Your
forloop has an empty update statement, this isn't wrong per-se but a bit unusual. Consider an alternative usingwhileinstead:int low = 0, high = numbers.length-1;
while (low < high)
int sum = numbers[low] + numbers[high];
//...- Insufficient test: As evidenced by its failure to catch the bug in 2. the test is too limited to be of much help. Consider checking more than one example (especially corner-cases like an empty input array) and checking against the expected output instead of just its size.
ooooo...indeed typo it should benumbers:-)
â Exploring
Mar 25 at 13:23
add a comment |Â
up vote
3
down vote
- Typo in method name as pointed out by @Stingy. Should be
findAllPairs. - Accessing wrong array: You are reading values from
inputinstead of your sorted copynumbers. This is probably a typo. E.g.int sum = input[low] + input[high];should beint sum = numbers[low] + numbers[high]; Your
forloop has an empty update statement, this isn't wrong per-se but a bit unusual. Consider an alternative usingwhileinstead:int low = 0, high = numbers.length-1;
while (low < high)
int sum = numbers[low] + numbers[high];
//...- Insufficient test: As evidenced by its failure to catch the bug in 2. the test is too limited to be of much help. Consider checking more than one example (especially corner-cases like an empty input array) and checking against the expected output instead of just its size.
ooooo...indeed typo it should benumbers:-)
â Exploring
Mar 25 at 13:23
add a comment |Â
up vote
3
down vote
up vote
3
down vote
- Typo in method name as pointed out by @Stingy. Should be
findAllPairs. - Accessing wrong array: You are reading values from
inputinstead of your sorted copynumbers. This is probably a typo. E.g.int sum = input[low] + input[high];should beint sum = numbers[low] + numbers[high]; Your
forloop has an empty update statement, this isn't wrong per-se but a bit unusual. Consider an alternative usingwhileinstead:int low = 0, high = numbers.length-1;
while (low < high)
int sum = numbers[low] + numbers[high];
//...- Insufficient test: As evidenced by its failure to catch the bug in 2. the test is too limited to be of much help. Consider checking more than one example (especially corner-cases like an empty input array) and checking against the expected output instead of just its size.
- Typo in method name as pointed out by @Stingy. Should be
findAllPairs. - Accessing wrong array: You are reading values from
inputinstead of your sorted copynumbers. This is probably a typo. E.g.int sum = input[low] + input[high];should beint sum = numbers[low] + numbers[high]; Your
forloop has an empty update statement, this isn't wrong per-se but a bit unusual. Consider an alternative usingwhileinstead:int low = 0, high = numbers.length-1;
while (low < high)
int sum = numbers[low] + numbers[high];
//...- Insufficient test: As evidenced by its failure to catch the bug in 2. the test is too limited to be of much help. Consider checking more than one example (especially corner-cases like an empty input array) and checking against the expected output instead of just its size.
answered Mar 25 at 11:33
Wingblade
1786
1786
ooooo...indeed typo it should benumbers:-)
â Exploring
Mar 25 at 13:23
add a comment |Â
ooooo...indeed typo it should benumbers:-)
â Exploring
Mar 25 at 13:23
ooooo...indeed typo it should be
numbers :-)â Exploring
Mar 25 at 13:23
ooooo...indeed typo it should be
numbers :-)â Exploring
Mar 25 at 13:23
add a comment |Â
up vote
1
down vote
You can prevent duplicate pairs by incrementing/decrementing your counters not only once, but until they point to a number that is greater/lesser than the number they previously pointed to. That way, you can store the pairs in a List instead of a Set, which will probably be faster, because the List doesn't need to check whether it already contains a pair that is equal to the one being added.
Also, there is a typo in your method names findAllParis.
add a comment |Â
up vote
1
down vote
You can prevent duplicate pairs by incrementing/decrementing your counters not only once, but until they point to a number that is greater/lesser than the number they previously pointed to. That way, you can store the pairs in a List instead of a Set, which will probably be faster, because the List doesn't need to check whether it already contains a pair that is equal to the one being added.
Also, there is a typo in your method names findAllParis.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can prevent duplicate pairs by incrementing/decrementing your counters not only once, but until they point to a number that is greater/lesser than the number they previously pointed to. That way, you can store the pairs in a List instead of a Set, which will probably be faster, because the List doesn't need to check whether it already contains a pair that is equal to the one being added.
Also, there is a typo in your method names findAllParis.
You can prevent duplicate pairs by incrementing/decrementing your counters not only once, but until they point to a number that is greater/lesser than the number they previously pointed to. That way, you can store the pairs in a List instead of a Set, which will probably be faster, because the List doesn't need to check whether it already contains a pair that is equal to the one being added.
Also, there is a typo in your method names findAllParis.
answered Mar 25 at 10:42
Stingy
1,888212
1,888212
add a comment |Â
add a comment |Â
up vote
0
down vote
accepted
Incorporating the feedbacks, here is the answer
Github: https://github.com/Ramblers-Code/CodeKata/blob/master/src/main/java/kata/array/TwoSumProblemUsingBinarySearch.java#18
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllPairs(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = numbers.length - 1; low < high; )
int sum = numbers[low] + numbers[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
add a comment |Â
up vote
0
down vote
accepted
Incorporating the feedbacks, here is the answer
Github: https://github.com/Ramblers-Code/CodeKata/blob/master/src/main/java/kata/array/TwoSumProblemUsingBinarySearch.java#18
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllPairs(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = numbers.length - 1; low < high; )
int sum = numbers[low] + numbers[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Incorporating the feedbacks, here is the answer
Github: https://github.com/Ramblers-Code/CodeKata/blob/master/src/main/java/kata/array/TwoSumProblemUsingBinarySearch.java#18
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllPairs(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = numbers.length - 1; low < high; )
int sum = numbers[low] + numbers[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
Incorporating the feedbacks, here is the answer
Github: https://github.com/Ramblers-Code/CodeKata/blob/master/src/main/java/kata/array/TwoSumProblemUsingBinarySearch.java#18
public class TwoSumProblemUsingBinarySearch
public static class Pair
private final int x;
private final int y;
public Pair(int x, int y)
this.x = x;
this.y = y;
@Override
public int hashCode()
return Objects.hash(x, y);
@Override
public boolean equals(Object other)
if (other instanceof Pair)
Pair o = (Pair) other;
return this.x == o.x && this.y == o.y;
return false;
@Override
public String toString()
return String.format("(%d, %d)", x, y);
public static Set<Pair> findAllPairs(int input, int target)
int numbers = Arrays.copyOf(input, input.length);
Set<Pair> pairs = new HashSet<>();
Arrays.sort(numbers);
for (int low = 0, high = numbers.length - 1; low < high; )
int sum = numbers[low] + numbers[high];
if (sum > target)
high--;
else if (sum < target)
low++;
else
pairs.add(new Pair(input[low], input[high]));
high--;
low++;
return pairs;
answered Mar 26 at 18:29
Exploring
20512
20512
add a comment |Â
add a comment |Â
up vote
-2
down vote
You would also have to add this to the findAllPairs method to sort the initial list which is the input:
Arrays.sort(input);
add a comment |Â
up vote
-2
down vote
You would also have to add this to the findAllPairs method to sort the initial list which is the input:
Arrays.sort(input);
add a comment |Â
up vote
-2
down vote
up vote
-2
down vote
You would also have to add this to the findAllPairs method to sort the initial list which is the input:
Arrays.sort(input);
You would also have to add this to the findAllPairs method to sort the initial list which is the input:
Arrays.sort(input);
edited Aug 2 at 4:40
Jamalâ¦
30.1k11114225
30.1k11114225
answered Aug 1 at 23:30
Megha
1
1
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190395%2ffind-all-pairs-in-an-array-that-sum-to-a-given-number-without-using-hashmap%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password