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 n, int a[]){vector<int> v;int max1 = a[n - 1];v.push_back(a[n - 1]);for (int i = n - 2; i >= 0; i--){if (a[i] >= max1){v.push_back(a[i]);max1 = max(max1, a[i]);}}for (int i = v.size() - 1; i >= 0; i--)cout << v[i] << " ";}int main(){int n = 6;int a[n] = {16, 17, 4, 3, 5, 2};hamiltonianAndLagrangian(n, a);return 0;}
No comments:
Post a Comment