Leaders in array
Given an integer array A of size n. Find and print all the leaders present in the input array. An array element A[i] is called Leader, if all the elements following it (i.e. present at its right) are less than or equal to A[i].
Print all the leader elements separated by space and in the same order they are present in the input array.
Sample Input 1 :
Sample Output 1 :
import java.util.Scanner;
public class LeaderArray {
public static void main(String[] args)
{
// int input[]= {1, 4, 9, 5, 3, 2, 0};
//int input[]= {13,17,5,4,6};
// leaders(input);
//
//public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int input[] = new int[n];
for(int i = 0; i < n; i++)
{
input[i] = s.nextInt();
}
leaders(input);
}
//
public static void leaders(int[] input)
{
int l=input.length;
for(int i=0;i<l;i++)
{
boolean isDec=true;
for(int j=i+1;j<l;j++)
{
if(input[i]<input[j])
{
isDec=false;
break;
}
}
if(isDec==true)
{
System.out.print(input[i]+ " ");
}
}
}
}
Comments
Post a Comment