Write a Java program to print last LEADERS in the array. Go to the editor Note: An element is leader if it is greater than all the elements to its right side.
5 1 4 3 9 4
Constraints
read n value and n number of integers
Output Format
9
Sample Input 0
5 1 2 3 9 5
Sample Output 0
9
Sample Input 1
10 12 3 1 19 1 1 56 4 3 2
Sample Output 1
56
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
int i,n;
int lno;
int no[];
Scanner s=new Scanner(System.in);
n=s.nextInt();
no=new int[n];
for(i=0;i<n;i++)
no[i]=s.nextInt();
int psum=0,j;
lno=no[0];
for(i=1;i<no.length;i++)
{
psum=0;
for(j=0;j<i;j++)
{
psum+=no[j];
}
if(no[i]>=psum)
lno=no[j];
}
System.out.println(lno);
}
}
No comments:
Post a Comment