Skip to content

Instantly share code, notes, and snippets.

@alexradzin
Last active June 12, 2022 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexradzin/c8a92b349c24e2d1d8eb85481ba43db2 to your computer and use it in GitHub Desktop.
Save alexradzin/c8a92b349c24e2d1d8eb85481ba43db2 to your computer and use it in GitHub Desktop.
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
package com.alexr.exercise;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
*
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
*
* You can return the answer in any order.
* Example 1
* Input: nums = [2,7,11,15], target = 9
* Output: [0,1]
* Output: Because nums[0] + nums[1] == 9, we return [0, 1].
* Example 2
* Input: nums = [3,2,4], target = 6
* Output: [1,2]
* Example 3
* Input: nums = [3,3], target = 6
* Output: [0,1]
*/
public class TaskTest {
public static int[] findSumEqualToTarget(int[] arr, int target) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
map.merge(arr[i], List.of(i), (existing, addition) -> Stream.of(existing, addition).flatMap(Collection::stream).collect(toList()));
}
for (Map.Entry<Integer, List<Integer>> e : map.entrySet()) {
int value = e.getKey();
int pair = target - value;
List<Integer> indexes2 = map.getOrDefault(pair, List.of());
int index1 = e.getValue().get(0);
int nIndexes = indexes2.size();
Integer index2;
if (nIndexes == 1) {
index2 = indexes2.get(0);
return new int[] {index1, index2};
} else if (nIndexes > 1) {
Optional<Integer> i2Opt = indexes2.stream().filter(i -> i != index1).findFirst();
if (i2Opt.isPresent()) {
return new int[] {index1, i2Opt.get()};
}
}
}
return null;
}
@Test
void empty() {
assertNull(findSumEqualToTarget(new int[0], 5));
}
@Test
void one() {
assertNull(findSumEqualToTarget(new int[] {5}, 5));
}
@Test
void twoMatching() {
assertArrayEquals(new int[] {0, 1}, findSumEqualToTarget(new int[] {2,3}, 5));
}
@Test
void twoNotMatching() {
assertNull(findSumEqualToTarget(new int[]{2, 3}, 7));
}
@Test
void example1() {
assertArrayEquals(new int[]{0, 1}, findSumEqualToTarget(new int[]{2, 7, 11, 15}, 9));
}
@Test
void example2() {
assertArrayEquals(new int[]{1, 2}, findSumEqualToTarget(new int[]{3,2,4}, 6));
}
@Test
void example3() {
assertArrayEquals(new int[]{0, 1}, findSumEqualToTarget(new int[]{3,3}, 6));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment