Last active
November 6, 2017 08:42
-
-
Save mayanknc/7f7c82e6938e226c3a279d77e7c1c7c2 to your computer and use it in GitHub Desktop.
LeaderInArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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