-
Notifications
You must be signed in to change notification settings - Fork 71
/
first-and-second-smallest-element.cpp
55 lines (51 loc) · 1.26 KB
/
first-and-second-smallest-element.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
// https://practice.geeksforgeeks.org/problems/find-the-smallest-and-second-smallest-element-in-an-array/0/?company[]=Goldman%20Sachs&problemStatus=unsolved&difficulty[]=-1&page=1&query=company[]Goldman%20SachsproblemStatusunsolveddifficulty[]-1page1#
#include <bits/stdc++.h>
using namespace std;
void solve_test()
{
int n;
cin >> n;
int arr[n];
for(int i=0; i< n; i++)
cin >> arr[i];
if(n <= 1)
cout <<"-1"<<endl;
else
{
sort(arr, arr+n);
// CHeck if there are two numbers
if(n == 2)
{
if(arr[0] == arr[1])
cout << "-1"<<endl;
else
cout << arr[0] << ' ' << arr[1]<<endl;
}
// There are > than 2 element, search first and second smallest;
int smallest = arr[0];
int biggest = smallest;
int i = 1;
while(i < n)
{
if(arr[i] != arr[0])
{
cout << smallest << ' ' << arr[i] <<endl;
return;
}
i+=1;
}
cout<< "-1" <<endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long int t;
cin >> t;
while(t--)
{
solve_test();
}
return 0;
}