Skip to content

Instantly share code, notes, and snippets.

@mayanknc
Last active November 6, 2017 08:42
Show Gist options
  • Save mayanknc/7f7c82e6938e226c3a279d77e7c1c7c2 to your computer and use it in GitHub Desktop.
Save mayanknc/7f7c82e6938e226c3a279d77e7c1c7c2 to your computer and use it in GitHub Desktop.
LeaderInArray
import java.util.Scanner;
import java.util.Stack;
public class LeaderInArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt(),n;
for(int tc=0;tc<t;tc++){
n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Stack<Integer> st = new Stack<Integer>();
int max = 0;
for(int j=n-1;j>=0;j--){
if(j==n-1){
max = a[n-1];
st.add(a[j]);
}
else{
if(max<=a[j]){
max = a[j];
st.add(a[j]);
}
}
}
while(!st.empty()){
System.out.println(st.pop());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment