-
Notifications
You must be signed in to change notification settings - Fork 1
/
bear.java
86 lines (75 loc) · 2.23 KB
/
bear.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
// Please name your class Main
class Main {
static void printpairs(int A[],int sum)
{ int arr_size=A.length;
int l, r;
int count=0;
Arrays.sort(A);
l = 0;
r = arr_size-1;
while (l < r)
{
if(A[l] + A[r] == sum)
{
System.out.println(A[l]+" "+A[r]);
count++;
break;
}
else if(A[l] + A[r] < sum)
{l++;}
else // A[i] + A[j] > sum
{r--; }
if(count==0 && l==r)
{
System.out.println("!OK");
}
}
}
// Main to test the above function
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int a = in.nextInt(); //number of cases
for(int i=0;i<a;i++)
{
try {
int S=in.nextInt();//sum to be checked
int E=in.nextInt(); //no of elements in list
int[] intArray = new int[E]; // allocating memory to array
for(int j=0;j<E;j++)
{
try {
//for reading array
intArray[j]=in.nextInt();
} catch (Exception e) {
System.out.println("!OK");
continue;
}
}
Arrays.sort(intArray);
if(E==0){//1
System.out.println("!OK");
}
else if(E==1){//2
if(intArray[0]==S){
System.out.println(S);
}
else{
System.out.println("!OK");
}
}
else{//3
printpairs(intArray, S);
}
} catch (Exception ex) {
System.out.println("!OK");
continue;
}
}
}
}