Skip to content

Commit 582f95e

Browse files
committed
Hamiltonian and Lagrangian
1 parent f6a0a1e commit 582f95e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Problem Link: https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/hamiltonian-and-lagrangian/
3+
4+
Students have become secret admirers of SEGP. They find the course exciting and the professors amusing.
5+
After a superb Mid Semester examination its now time for the results. The TAs have released the
6+
marks of students in the form of an array, where arr[i] represents the marks of the ith student.
7+
Since you are a curious kid, you want to find all the marks that are not smaller than those on its
8+
right side in the array.
9+
10+
Input Format
11+
The first line of input will contain a single integer n denoting the number of students.
12+
The next line will contain n space separated integers representing the marks of students.
13+
14+
Output Format
15+
Output all the integers separated in the array from left to right that are not smaller than
16+
those on its right side.
17+
18+
Constraints
19+
1 <= n <= 1000000
20+
0 <= arr[i] <= 10000
21+
22+
SAMPLE INPUT
23+
6
24+
16 17 4 3 5 2
25+
SAMPLE OUTPUT
26+
17 5 2
27+
"""
28+
def maxMarks(marks):
29+
res = []
30+
maxMark = -1
31+
for mark in marks[::-1]:
32+
if mark >= maxMark:
33+
maxMark = mark
34+
res.append(mark)
35+
return res[::-1]
36+
37+
n = int(input())
38+
marks = list(map(int,input().split()))
39+
print(*maxMarks(marks))

0 commit comments

Comments
 (0)