-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathImmediate Smaller Element
More file actions
52 lines (43 loc) · 1.39 KB
/
Immediate Smaller Element
File metadata and controls
52 lines (43 loc) · 1.39 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Given an integer array of size N. For each element in the array, check whether there exist a smaller element on the next immediate position of
the array. If such an element exists, print that element. If there is no smaller element on the immediate next to the element then print -1.
Input:
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains 2 lines of input:
The first line contains an integer N, where N is the size of array.
The second line contains N integers(elements of the array) sperated with spaces.
Output:
For each test case, print the next immediate smaller elements for each element in the array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ arr[i] ≤ 1000
Example:
Input
2
5
4 2 1 5 3
6
5 6 2 3 1 7
Output
2 1 -1 3 -1
-1 2 -1 1 -1 -1
Explanation:
Testcase 1: Array elements are 4, 2, 1, 5, 3. Immediate smaller of 2 is immediate smaller of 4, 1 is immediate smaller of 2,
no immediate smaller of 1,3 is immediate smaller of 5, and no immediate smaller for last element exists. So ouput is : 2 1 -1 3 -1.*/
//Program
#include <iostream>
using namespace std;
int main() {
int t; cin>>t; while(t--)
{
int n; cin>>n;
int a[n],i; for(i=0;i<n;i++) cin>>a[i];
for(i=0;i<n-1;i++)
{
if(a[i+1]<a[i]) cout<<a[i+1]<<" ";
else cout<<-1<<" ";
}
cout<<-1<<endl;
}
return 0;
}