Given a list of N integers, find the maximum and minimum element’s position in the list.
Examples:
Input : 3, 4, 1, 3, 4, 5
Output : The maximum is at position 6
The minimum is at position 3
The naive approach will be to traverse in the list and keep track of the minimum and maximum along with their indices. We have to do N comparisons for minimum and at the same time N comparisons for maximum.
Below is the implementation of the naive approach.
#Naive approach #to find index of maximum and #minimum element in the array. gfg_list=[8,1,7,10,5] #min and max indexes are taken 1st element #In some cases list might be a single element min_ele,max_ele=gfg_list[0],gfg_list[0] for i in range(1,len(gfg_list)): if gfg_list[i]<min_ele: min_ele=gfg_list[i] if gfg_list[i]>max_ele: max_ele=gfg_list[i] print('Minimum Element in the list',gfg_list,'is',min_ele) print('Maximum Element in the list',gfg_list,'is',max_ele) |
Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 10
Python inbuilt function allows us to find it in one line, we can find the minimum in the list using the min() function and then us index() function to find out the index of that minimum element. Similarly we can do the same for finding out the maximum element using max() function and then find out the index of the maximum element using index() inbuilt function in python.
Note : index() returns index of first occurrence in case there are multiple occurrences of an element. So if maximum (or minimum) occurs more than once, the first occurrence is returned.
Below is the implementation of the above approach:
# function to find minimum and maximum position in list def minimum(a, n): # inbuilt function to find the position of minimum minpos = a.index(min(a)) # inbuilt function to find the position of maximum maxpos = a.index(max(a)) # printing the position print "The maximum is at position", maxpos + 1 print "The minimum is at position", minpos + 1 # driver code a = [3, 4, 1, 3, 4, 5] minimum(a, len(a)) |
The maximum is at position 6 The minimum is at position 3
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Position Summation in List of Tuples
- Python | Shift last element to first position in list
- Python | Find frequency of given character at every position in list of lists
- Python | Selective Merge list every Nth position
- Python | Add element at alternate position in list
- Python program to find the character position of Kth word from a list of strings
- Find maximum values & position in columns and rows of a Dataframe in Pandas
- Get minimum values in rows or columns with their index position in Pandas-Dataframe
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python | Maximum sum of elements of list in a list of lists
- Python | Find maximum length sub-list in a nested list
- Python | Maximum absolute difference list of list
- Python - Change kivy button size and position using kv file
- Python | Reverse an array upto a given position
- Python Slicing | Extract ‘k’ bits from a given position
- Python | Find position of a character in given string
- Python - Test if Substring occurs in specific position
- Python | Filter String with substring at specific position
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

