-
Notifications
You must be signed in to change notification settings - Fork 0
/
twoSum.java
51 lines (43 loc) · 1.4 KB
/
twoSum.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
// time complexity: O(n)
// space complexity: O(n)
import java.util.*;
public class twoSum {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an array");
int n = sc.nextInt();
// input array elements in an array
int[] arr = new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
// input target element in an array
System.out.println("Enter the target element in an array");
int target = sc.nextInt();
// Create a hashmap where key is arr[i] and value is i
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0; i<n; i++){
map.put(arr[i], i);
}
// create a result array to display the index of two numbers
int result[] = new int[2];
// if current is equal to the target
for(int i=0; i<n; i++){
if(arr[i] == target && map.containsKey(0)){
result[0] = i;
result[1] = map.get(0);
break;
}
else if(map.containsKey(target - arr[i])){
// is the element non-repeatable
if(map.get(target - arr[i]) > i){
result[0] = i;
result[1] = map.get(target - arr[i]);
break;
}
}
}
System.out.println("Two Sum index values are: ["+result[0]+","+result[1]+"]");
}
}