forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
27 lines (23 loc) · 683 Bytes
/
Copy pathTwoSum.java
File metadata and controls
27 lines (23 loc) · 683 Bytes
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
public class TwoSum {
public static void main(String[] args) {
int[] arr = {2,3,4,5,6,7};
int x = 11;
int[] res = twoSum(arr,x);
for(int i = 0 ; i< res.length;i++){
System.out.println(res[i]);
}
}
public static int[] twoSum(int[] nums, int target) {
int a[] = new int[2];
for(int i = 0; i < nums.length - 1; i++){
for(int j = i + 1; j < nums.length; j++){
if(target == nums[i] + nums[j]){
a[0] = i;
a[1] = j;
break;
}
}
}
return a;
}
}