-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy paththe-smallest-pair.cpp
66 lines (61 loc) · 1.13 KB
/
the-smallest-pair.cpp
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
// https://www.codechef.com/problems/SMPAIR
#include<iostream>
#include <algorithm>
using namespace std;
void fill_array(int *array, int n)
{
int i;
int number;
for(i = 0; i< n; i++)
{
cin >> number;
array[i] = number;
}
/*array[0] = 5;
array[1] = 1;
array[2] = 3;
array[3] = 4;
*/
}
int solve_test()
{
int n;
cin >> n;
int arr[n];
fill_array(arr, n);
/*
for(int i =0; i< n; i++)
cout << arr[i]<< " ";
cout <<endl;
*/
sort(arr, arr+n);
// If N = 2;
if(n==2)
{
if(arr[0] + arr[1] <= n)
return arr[0]+arr[1];
else
return -1;
}
int sum;
for(int i =0; i < n-1; i++)
{
for(int j= i+1; j<n; j++)
{
sum = arr[i] + arr[j];
//cout <<"i: "<<i << " j: "<<j << " sum: "<<sum<<endl;
if( i < j && sum <=n)
return sum;
}
}
// Something went wrong
return -1;
}
int main()
{
int t;
cin >> t;
while(t--)
cout<<solve_test()<<endl;
return 0;
}