-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_01.java
59 lines (55 loc) · 1.36 KB
/
Problem_01.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* LeetCode
* Problem_01.java
*/
package com.deepak.leetcode.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* <br> Problem Statement :
*
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
*
* Example:
* Given nums = [2, 7, 11, 15], target = 9,
*
* Because nums[0] + nums[1] = 2 + 7 = 9,
* return [0, 1]
*
* </br>
*
* @author Deepak
*/
public class Problem_01 {
/**
* Method to return indices of two numbers which add up to a specific target
*
* Time Complexity : O(n) => Where n is number of elements in input array
* Space Complexity : O(n) => In the worst case, all elements will be added to map
*
* @param nums
* @param target
* @return {@link int[]}
*/
public static int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
if (nums == null || nums.length < 2) {
return result;
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
result[0] = map.get(target - nums[i]);
result[1] = i;
return result;
}
/* Handle Duplicates */
if (map.containsKey(target - nums[i])) {
continue;
}
map.put(nums[i], i);
}
return result;
}
}