Skip to content

Commit 34cf6da

Browse files
add two sum problem (TheAlgorithms#4364)
* add two sum problem * linter solved * linter solved * improve code * linter solved * improve code * mini linter solved * update code --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
1 parent 94621fb commit 34cf6da

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.misc;
2+
3+
import java.util.HashMap;
4+
import java.util.Optional;
5+
import org.apache.commons.lang3.tuple.Pair;
6+
7+
public final class TwoSumProblem {
8+
private TwoSumProblem() {
9+
}
10+
11+
/**
12+
* The function "twoSum" takes an array of integers and a target integer as input, and returns an
13+
* array of two indices where the corresponding elements in the input array add up to the target.
14+
* @param values An array of integers.
15+
* @param target The target is the sum that we are trying to find using two numbers from the given array.
16+
* @return A pair or indexes such that sum of values at these indexes equals to the target
17+
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
18+
*/
19+
20+
public static Optional<Pair<Integer, Integer>> twoSum(final int[] values, final int target) {
21+
HashMap<Integer, Integer> valueToIndex = new HashMap<>();
22+
for (int i = 0; i < values.length; i++) {
23+
final var rem = target - values[i];
24+
if (valueToIndex.containsKey(rem)) {
25+
return Optional.of(Pair.of(valueToIndex.get(rem), i));
26+
}
27+
if (!valueToIndex.containsKey(values[i])) {
28+
valueToIndex.put(values[i], i);
29+
}
30+
}
31+
return Optional.empty();
32+
}
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
6+
import org.apache.commons.lang3.tuple.Pair;
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Test case for Two sum Problem.
11+
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
12+
*/
13+
14+
public class TwoSumProblemTest {
15+
16+
@Test
17+
void testTwoSumExists() {
18+
final int[] values = new int[] {2, 7, 11, 15};
19+
final int target = 9;
20+
final var expected = Pair.of(0, 1); // values[0] + values[1] = 2 + 7 = 9
21+
assertEquals(expected, TwoSumProblem.twoSum(values, target).get());
22+
}
23+
24+
@Test
25+
void testTwoSumNoSolution() {
26+
final int[] values = new int[] {2, 7, 11, 15};
27+
final int target = 3;
28+
assertFalse(TwoSumProblem.twoSum(values, target).isPresent());
29+
}
30+
31+
@Test
32+
void testTwoSumMultipleSolutions() {
33+
final int[] values = {3, 3};
34+
final int target = 6;
35+
final var expected = Pair.of(0, 1); // values[0] + values[1] = 3 + 3 = 6
36+
assertEquals(expected, TwoSumProblem.twoSum(values, target).get());
37+
}
38+
39+
@Test
40+
void testTwoSumMultipleSolution() {
41+
final int[] values = {3, 4, 3, 3};
42+
final int target = 6;
43+
final var expected = Pair.of(0, 2); // values[0] + values[2] = 3 + 3 = 6
44+
assertEquals(expected, TwoSumProblem.twoSum(values, target).get());
45+
}
46+
47+
@Test
48+
void testTwoSumNegativeNumbers() {
49+
final int[] values = {-1, -2, -3, -4, -5};
50+
final int target = -8;
51+
final var expected = Pair.of(2, 4); // values[2] + values[4] = -3 + (-5) = -8
52+
assertEquals(expected, TwoSumProblem.twoSum(values, target).get());
53+
}
54+
55+
@Test
56+
void testTwoSumNoSolutionDuplicatedInputs() {
57+
final int[] values = {0, 0, 0};
58+
final int target = 100;
59+
assertFalse(TwoSumProblem.twoSum(values, target).isPresent());
60+
}
61+
}

0 commit comments

Comments
 (0)