Skip to content

Instantly share code, notes, and snippets.

@mk6619
Created December 4, 2019 13:18
Show Gist options
  • Select an option

  • Save mk6619/3949329dbd39fa35921612e9f35c82ab to your computer and use it in GitHub Desktop.

Select an option

Save mk6619/3949329dbd39fa35921612e9f35c82ab to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main() {
//code
int testCases;
cin>>testCases;
for(int i = 0 ; i < testCases; i++) {
int n;
cin>>n;
int arr[n];
for(int j =0 ; j < n ; j++) {
cin>>arr[j];
}
int count = n-2;
int arr_m[n];
arr_m[n-1] = arr[n-1];
int maxElement = arr[n-1];
for(int j =n-2 ; j >= 0 ; j--) {
if(arr[j] >= maxElement) {
arr_m[count] = arr[j];
count--;
maxElement = arr[j];
}
}
for(int k = count+1; k < n; k++){
cout<<arr_m[k]<<" ";
}
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment