# Next Greater

QUESTION

Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider next greater element as -1.\n\nExamples:\na) For any array, rightmost element always has next greater element as -1.\nb) For an array which is sorted in decreasing order, all elements have next greater element as -1.\nc) For the input array [4, 5, 2, 25}, the next greater elements for each element are as follows.\n\nElement NGE\n4 –> 5\n5 –> 25\n2 –> 25\n25 –> -1\n\nINPUT:\nThe first line of input contains size of the array\nThe second line contains of the elements of the array.\n\nOutput:\nThe output contains resultant array.

“TESTCASE_1”: “4\n4 5 2 25\n###—###SEPERATOR—###—\n5\n25\n25\n-1”, “TESTCASE_2”: “4\n13 7 6 12\n###—###SEPERATOR—###—\n-1\n12\n12\n-1”, “TESTCASE_3”: “6\n1 8 13 4 89 2\n###—###SEPERATOR—###—\n8\n13\n89\n89\n-1\n-1”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

``````#include <iostream>
using namespace std;
int main()
{
int n,i,j,a[15],g[15];
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
int flag=0;
for(j=i+1;j<n;j++){
if(a[j]>a[i])
{
flag=1;
break;
}
}
if(flag==1)
cout<<a[j]<<endl;
else
cout<<-1<<endl;
}

return 0;
}``````