Given a list of N integers, find the maximum and minimum element’s position in the list.
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.
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:
The maximum is at position 6 The minimum is at position 3
- Python | Position Summation in List of Tuples
- Python | Add element at alternate position in list
- Python | Selective Merge list every Nth position
- Python | Shift last element to first position in list
- Python | Find frequency of given character at every position in list of lists
- Python | Find maximum length sub-list in a nested list
- Python | Maximum sum of elements of list in a list of lists
- Python | Maximum absolute difference list of list
- Python | Maximum and Minimum in a Set
- Python - Minimum in tuple list value
- Python | List of tuples Minimum
- Python - Minimum in each record value list
- Python | Cropped list Minimum
- Python - Every Kth index Maximum in List
- Python | Maximum and Minimum value from two lists
- Python - Minimum Product Pair in List
- Python - Alternate Minimum element in list
- Python - Column Minimum in Tuple list
- Python | Minimum element in tuple list
- Python - Find Minimum Pair Sum in list