-
Notifications
You must be signed in to change notification settings - Fork 0
/
twosums.java
46 lines (37 loc) · 1.08 KB
/
twosums.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
// public class twosums{
// public static void twosumproblem(int[] arr, int target) {
// int sum=0;
// for(int i=0;i<arr.length;i++){
// for(int j=i+1;j<arr.length;j++){
// sum = arr[i]+arr[j];
// if(sum == target){
// System.out.println("["+i+" "+j+"]");
// }
// }
// }
// }
// public static void main(String[] args) {
// int arr[] = {3,3};
// twosumproblem(arr,6);
// }
// }
class twosums {
public static int[] twoSum(int[] nums, int target) {
int sum =0;
int[] result = new int[nums.length];
// int target = 6;
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
sum = nums[i]+nums[j];
if(sum == target){
System.out.println("["+i+" "+j+"]");
}
}
}
return result;
}
public static void main(String[] args) {
int[] nums = {3,2,4};
twoSum(nums,6);
}
}