-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoNumberWithGivenSum.java
More file actions
55 lines (51 loc) · 1.24 KB
/
TwoNumberWithGivenSum.java
File metadata and controls
55 lines (51 loc) · 1.24 KB
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
package algorithm.search;
import java.util.HashMap;
import java.util.Map;
/**
* The type Two number with given sum.
*/
public class TwoNumberWithGivenSum {
/**
* Two numbers int [ ].
*
* @param arr the arr
* @param sum the sum
* @return the int [ ]
*/
public static int[] twoNumbers(int[] arr, int sum){
int[] ans = new int[2];
Map map = new HashMap();
int l = arr.length;
for(int i=0;i<l;i++){
map.put(arr[i],1);
if(null != map.get(sum-arr[i])){
ans[0] = arr[i];
ans[1] = sum - arr[i];
return ans;
}
}
return ans;
}
/**
* Quick sort and binary search int [ ].
*
* @param arr the arr
* @param sum the sum
* @return the int [ ]
*/
public static int[] quickSortAndBinarySearch(int[] arr, int sum){
int[] ans = new int[2];
return ans;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String[] args){
int[] arr = {1,21,3,14,5,60,7,6};
int sum = 81;
int[] ans = twoNumbers(arr,sum);
System.out.println("The two numbers are :: "+ans[1]+" and "+ans[0]);
}
}