Header Ads Widget

Hamiltonian and Lagrangian

 Problem:-

Students have become secret admirers of SEGP. They find the course exciting and the professors amusing. After a superb Mid Semester examination its now time for the results. The TAs have released the marks of students in the form of an array, where arr[i] represents the marks of the ith student.

Since you are a curious kid, you want to find all the marks that are not smaller than those on its right side in the array.

Input Format
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.

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

Constraints
1 <= n <= 1000000
0 <= arr[i] <= 10000

Problem Setter - Dhvit

Sample Input
6
16 17 4 3 5 2
Sample Output
17 5 2
Time Limit: 0.5
Memory Limit: 256
Source Limit:

Code:-

#include<stdio.h>
long long int stack[1000000],top=-1;
int main()
{
    long long int n,a[1000000],j,i;
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
    }
    top++;
    stack[top]=a[n-1];
    for(i=n-2;i>=0;i--)
    {
if(a[i]>=stack[top])
        {
            top++;
            stack[top]=a[i];
        }
        
    }
    for(i=top;i>=0;i--)
     printf("%lld ",stack[i]);
    return 0;
}



Post a Comment

0 Comments