-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.java
More file actions
31 lines (29 loc) · 1.08 KB
/
twoSum.java
File metadata and controls
31 lines (29 loc) · 1.08 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
// Question: Find the doublet in the Array whose sum is equal to the given value x. (Two Sum)
// ( if the Array is {5, 2, 7, -1} and x = 9 then the doublet is [2 and 7] )
package arrays;
import java.util.Scanner;
public class twoSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array: ");
int n = sc.nextInt();
int [] arr= new int[n];
System.out.print("Enter the elements of the array: ");
for(int i=0; i<n; i++){
arr[i]=sc.nextInt();
}
System.out.print("Your array is: ");
for(int ele : arr){
System.out.print(ele+" ");
}
System.out.println();
System.out.print("Enter a integer(x): ");
int x=sc.nextInt();
System.out.println("The duplets whose sum is equal to the given integer are: ");
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(arr[i]+arr[j]==x){System.out.println(arr[i]+" , "+arr[j]);}
}
}
}
}