Find the first, second and third minimum elements in an array in O(n).
Input : 9 4 12 6 Output : First min = 4 Second min = 6 Third min = 9 Input : 4 9 1 32 12 Output : First min = 1 Second min = 4 Third min = 9
First approach : First we can use normal method that is sort the array and then print first, second and third element of the array. Time complexity of this solution is O(n Log n).
Second approach : Time complexity of this solution is O(n).
First take an element then if array[index] < Firstelement Thirdelement = Secondelement Secondelement = Firstelement Firstelement = array[index] else if array[index] < Secondelement Thirdelement = Secondelement Secondelement = array[index] else if array[index] < Thirdelement Thirdelement = array[index] then print all the element
First min = 1 Second min = 4 Third min = 9
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find minimum value to assign all array elements so that array product becomes greater
- Find the minimum and maximum sum of N-1 elements of the array
- Find minimum changes required in an array for it to contain k distinct elements
- Recursive Programs to find Minimum and Maximum elements of array
- Find the minimum number of elements that should be removed to make an array good
- Find the minimum number of operations required to make all array elements equal
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Merge first two minimum elements of the array until all the elements are greater than K
- Find original array from encrypted array (An array of sums of other elements)
- Find elements larger than half of the elements in an array
- Find minimum sum such that one of every three consecutive elements is taken
- Find minimum difference between any two elements
- Find minimum elements after considering all possible transformations
- Find minimum difference between any two elements | Set 2
- Remove minimum elements from array so that max <= 2 * min
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.