Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N.
Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52]
A simple solution traverse the given list N times. In every traversal, find the maximum, add it to result, and remove it from the list. Below is the implementation :
Time Complexity : O(N * size) where size is size of the given list.
Please refer k largest(or smallest) elements in an array for more efficient solutions of this problem.
- Python program to find second largest number in a list
- Python program to find largest number in a list
- Python program to find sum of elements in list
- Python | Indices of N largest elements in list
- Python Program to find largest element in an array
- Python program to swap two elements in a list
- Python program to interchange first and last elements in a list
- Python Program for Find largest prime factor of a number
- Python | Find elements of a list by indices
- Python | Find all elements count in list
- Python | Find missing elements in List
- Python | Find sum of frequency of given elements in the list
- Python | Find common elements in list of lists
- Python | Find top K frequent elements from a list of tuples
- Python | Find the list elements starting with specific letter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.