
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the element before which all the elements are smaller than it, and after which all are greater in Python
Suppose we have an array, we have to find an element before which all elements are less than it, and after which all are greater than it. Finally, return the index of the element, if there is no such element, then return -1.
So, if the input is like A - [6, 2, 5, 4, 7, 9, 11, 8, 10], then the output will be 4.
To solve this, we will follow these steps −
n := size of arr
maximum_left := an array of size n
maximum_left[0] := -infinity
for i in range 1 to n, do
maximum_left[i] := maximum of maximum_left[i-1], arr[i-1]
minimum_right := infinity
for i in range n-1 to -1, decrease by 1, do
if maximum_left[i] < arr[i] and minimum_right > arr[i], then
return i
minimum_right := minimum of minimum_right, arr[i]
return -1
Example
Let us see the following implementation to get better understanding −
def get_element(arr): n = len(arr) maximum_left = [None] * n maximum_left[0] = float('-inf') for i in range(1, n): maximum_left[i] = max(maximum_left[i-1], arr[i-1]) minimum_right = float('inf') for i in range(n-1, -1, -1): if maximum_left[i] < arr[i] and minimum_right > arr[i]: return i minimum_right = min(minimum_right, arr[i]) return -1 arr = [6, 2, 5, 4, 7, 9, 11, 8, 10] print(get_element(arr))
Input
[6, 2, 5, 4, 7, 9, 11, 8, 10]
Output
4
- Related Articles
- Find Array Elements Which are Greater Than Its Immediate Left Element?
- Find Array Elements Which are Greater than its Left Elements in Java?
- Find the Number of segments where all elements are greater than X using C++
- Program to filter all values which are greater than x in an array
- Which of the following statements are true (T) and which are false (F):All the angles of a triangle can be greater than $60^o$.
- Find all elements in array which have at-least two greater elements in C++
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
- Program to find number of elements in all permutation which are following given conditions in Python
- Which nutrient gives us more energy than others? Are all the same?
- Program to find number not greater than n where all digits are non-decreasing in python
- Find an array element such that all elements are divisible by it using c++
- Delete all the nodes from the list that are greater than x in C++
- Write five rational numbers which are smaller than 2.
- Write all the numbers less than 100 which are common multiples of 3 and 4.
- Count subarrays with all elements greater than K in C++
