Code Snippet
package com.ideserve.questions.saurabh;
/**
* <b>IDeserve <br>
* <a href="https://www.youtube.com/c/IDeserve">https://www.youtube.com/c/IDeserve</a>
* Given an array of integers, print the leaders in the array.
* A leader is an element which is larger than all the elements
* in the array to its right.
*
* @author Saurabh
*/
public class LeaderElements {
public static void printLeaders(int[] input) {
if(input == null || input.length == 0) {
return;
}
int inputSize = input.length;
int currentLeader = input[inputSize - 1];
System.out.print("Leaders- ");
System.out.print(currentLeader + " ");
for (int i = inputSize - 2; i >= 0; i--) {
if(input[i] > currentLeader) {
currentLeader = input[i];
System.out.print(currentLeader + " ");
}
}
}
public static void main(String[] args) {
int[] input = { 98, 23, 54, 12, 20, 7, 27 };
printLeaders(input);
}
}