-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelements_which_have_at_least_two_greater_elements.py
81 lines (77 loc) · 1.81 KB
/
elements_which_have_at_least_two_greater_elements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -----------------------------------------------
# Author : Mohit Chaudhari
# Created Date : 20/07/21
# -----------------------------------------------
# ***** Elements which have at-least two greater elements *****
# Problem Description
#
# You are given an array of distinct integers A, you have to find and return all elements in array which have at-least
# two greater elements than themselves.
#
# NOTE: The result should have the order in which they are present in the original array.
#
#
#
# Problem Constraints
# 3 <= |A| <= 105
#
# -109 <= A[i] <= 109
#
#
#
# Input Format
# First and only argument is an integer array A.
#
#
#
# Output Format
# Return an integer array containing the elements of A which have at-least two greater elements than themselves in A.
#
#
#
# Example Input
# Input 1:
#
# A = [1, 2, 3, 4, 5]
# Input 2:
#
# A = [11, 17, 100, 5]
#
#
# Example Output
# Output 1:
#
# [1, 2, 3]
# Output 2:
#
# [11, 5]
#
#
# Example Explanation
# Explanation 1:
#
# Number of elements greater than 1: 4
# Number of elements greater than 2: 3
# Number of elements greater than 3: 2
# Number of elements greater than 4: 1
# Number of elements greater than 5: 0
# Elements 1, 2 and 3 have atleast 2 elements strictly greater than themselves.
# Explanation 2:
#
# Number of elements greater than 11: 2
# Number of elements greater than 17: 1
# Number of elements greater than 100: 0
# Number of elements greater than 5: 3
# Elements 5 and 11 have atleast 2 elements strictly greater than themselves.
class Solution:
# @param A : list of integers
# @return a list of integers
def solve(self, A):
B = A.copy()
A.sort()
lst = A[len(A)-2:]
new_lst = [x for x in B if x not in lst]
return new_lst
s = Solution()
print(s.solve([1, 2, 3, 4, 5]))
# OUTPUT: [1, 2, 3]