-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwosum.java
More file actions
45 lines (30 loc) · 1.05 KB
/
twosum.java
File metadata and controls
45 lines (30 loc) · 1.05 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
import java.util.Scanner;
public class twosum {
public int[] twoSum(int[] nums, int target) {
int[] ans = {0,0};
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target){
ans[0]=j;
ans[1]=i;
}
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter the size");
int n= sc.nextInt();
System.out.println("Enter the target");
int target= sc.nextInt();
System.out.println("Enter the array");
int nums[] = new int[n];
for(int i=0;i<n;i++)
nums[i]= sc.nextInt();
twosum obj =new twosum();
System.out.println("Answer: ");
System.out.println(obj.twoSum(nums,target)[0]);
System.out.println(obj.twoSum(nums,target)[1]);
}
}