
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Interesting Python Implementation for Next Greater Elements
In this article, we will learn about defining and user-defined functions to predict the next greatest element.
Problem statement
We are given an array & we need to print the Next Greater Element for every element present in the array. The Next greater Element for an arbitrary element y is the first greatest element present on the right side of x in array. Elements for which no greatest element exist, return -1 as output.4
Input test case
[12,1,2,3]
Output
12 -> -1 1 -> 3 2 -> 3 3 -> -1
Now let’s observe the source code.
Example
# Function Def elevalue(arr): # Iteration for i in range(0, len(arr)): # slicing max final = max(arr[i:]) # greatest check if (arr[i] == final): print("% d -> % d" % (arr[i], -1)) else: print("% d -> % d" % (arr[i], final)) # Driver program def main(): arr = [12,1,2,3] elevalue(arr) arr = [1,34,2,1] elevalue(arr) if __name__ == '__main__': main()
Output
12 -> -1 1 -> 3 2 -> 3 3 -> -1 1 -> 34 34 -> -1 2 -> -1 1 -> -1
Conclusion
In this article, we learned about interesting python implementation for next greater elements by using a user-defined function.
- Related Articles
- Finding next greater node for each node in JavaScript
- Next Greater Element in C++
- How can I make Python interesting for me?
- Elements greater than the previous and next element in an Array in C++
- Python – Next N elements from K value
- Next Greater Element II in C++
- Next Greater Element III in C++
- Find next Smaller of next Greater in an array in C++
- Next Greater Element in Circular Array in JavaScript
- Finding distance to next greater element in JavaScript
- 10 Interesting Python Cool Tricks
- Interesting facts about strings in Python
- Python Alternate repr() implementation
- Program to find X for special array with X elements greater than or equal X in Python
- Next greater element in same order as input in C++

Advertisements