Hamiltonian and Lagrangian

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.

Example:

Input:  n = 6, a = [16, 17, 4, 3, 5, 2]
Output: 17 5 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void hamiltonianAndLagrangian(int nint a[])
{
    vector<intv;
    int max1 = a[n - 1];
    v.push_back(a[n - 1]);
    for (int i = n - 2i >= 0i--)
    {
        if (a[i] >= max1)
        {
            v.push_back(a[i]);
            max1 = max(max1a[i]);
        }
    }
    for (int i = v.size() - 1i >= 0i--)
        cout << v[i] << " ";
}
int main()
{
    int n = 6;

    int a[n] = {16174352};

    hamiltonianAndLagrangian(na);

    return 0;
}


No comments:

Post a Comment