Input 

The first line of input will contain a single integer n denoting the number of students.
The next line will contain n space-separated integers representing the marks of students.
6
16 17 4 3 5 2

Output
Output all the integers separated in the array from left to right that are not smaller than those on its right side.

17 5 2

Solution


import java.util.*;
class Hamiltonian{
   public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      int n=sc.nextInt();
      int a[]=new int[n];
int count=0;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
         }
for(int i=0;i<n-1;i++) //16 17 4 3 5 2
{
count=n-1-i;
for(int j=i+1;j<n;j++)
{
if(a[i]>=a[j])
    count=count-1;
else
break;
}
if(count==0)
  System.out.print(a[i]+" ");
}
System.out.print(a[n-1]);
   }

}


Post a Comment

Previous Post Next Post