## Next larger element

QUESTION

Given an array A [ ] having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array. If no such element exists, output -1 \n\nInput:\nThe first line of input contains a single integer T denoting the number of test cases.Then T test cases follow. Each test case consists of two lines. The first line contains an integer N denoting the size of the array. The Second line of each test case contains N space separated positive integers denoting the values/elements in the array A[ ].\n \nOutput:\nFor each test case, print in a new line, the next greater element for each array element separated by space in order.\n\nConstraints:\n1<=T<=100\n1<=N<=1000\n1<=A[i]<=1000.

“TESTCASE_1”: “1\n4\n1 3 2 4\n###—###SEPERATOR—###—\n3 4 4 -1”, “TESTCASE_2”: “1\n4\n5 6 4 9\n###—###SEPERATOR—###—\n6 9 9 -1”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

``````import java.io.*;
import java.util.*;
public class TestClass {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
for(int i=0;i<test;i++)
{
int n=sc.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
for(i=0;i<n;i++)
{
int next=-1;
for(int j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
next=arr[j];
break;
}
}
System.out.print(next+" ");
}
}
}
}``````