-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpair_with_given_sum.java
More file actions
43 lines (38 loc) · 886 Bytes
/
pair_with_given_sum.java
File metadata and controls
43 lines (38 loc) · 886 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Input -> 5
1 2 3 4 5
9
Output -> 9 sum is present by adding (5, 4)
*/
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int[] ar=new int[n];
int i,d,j,x=0,y=0,k=0,sum=0;
for(i=0;i<n;i++)
ar[i]=scan.nextInt();
d=scan.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
sum=ar[i]+ar[j];
if(sum==d)
{
k=1;
x=ar[i];
y=ar[j];
break;
}
}
}
if(k==1)
System.out.println(d+" sum is present by adding ("+x+", "+y+")");
else
System.out.println(d+" is not present");
}
}