Python – Maximum in Row Range
Last Updated :
08 Mar, 2023
Given a range and a Matrix, extract the maximum element out of that range of rows.
Input : test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]], i, j = 2, 5
Output : 12
Explanation : Checks for rows 2, 3 and 4, maximum element is 12.
Input : test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]], i, j = 1, 4
Output : 10
Explanation : Checks for rows 1, 2 and 3, maximum element is 10.
Method #1 : Using max() + slicing
In this, we perform the task of slicing the rows in which maximum has to be found, then the maximum is found for each row using max(), another max() is applied to get maximum upon extracted elements.
Python3
test_list = [[ 4 , 3 , 6 ], [ 9 , 1 , 3 ], [ 4 , 5 , 2 ],
[ 9 , 10 , 3 ], [ 5 , 9 , 12 ], [ 3 , 14 , 2 ]]
print ( "The original list is : " + str (test_list))
i, j = 2 , 4
res = 0
for idx in range (i, j):
res = max ( max (test_list[idx]), res)
print ( "The maximum element in row range ? : " + str (res))
|
Output
The original list is : [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]]
The maximum element in row range ? : 10
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using max() + slicing + list comprehension
In this, we perform a similar task as above using list comprehension to offer one-liner to this operation.
Python3
test_list = [[ 4 , 3 , 6 ], [ 9 , 1 , 3 ], [ 4 , 5 , 2 ],
[ 9 , 10 , 3 ], [ 5 , 9 , 12 ], [ 3 , 14 , 2 ]]
print ( "The original list is : " + str (test_list))
i, j = 2 , 4
res = max ([ max (test_list[idx]) for idx in range (i, j)])
print ( "The maximum element in row range ? : " + str (res))
|
Output
The original list is : [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]]
The maximum element in row range ? : 10
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in max() + slicing + list comprehension which all has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re not using any additional space other than the input list itself.
Method #3 : Using slicing+extend()+max()
Python3
test_list = [[ 4 , 3 , 6 ], [ 9 , 1 , 3 ], [ 4 , 5 , 2 ],
[ 9 , 10 , 3 ], [ 5 , 9 , 12 ], [ 3 , 14 , 2 ]]
print ( "The original list is : " + str (test_list))
i, j = 2 , 4
res = 0
x = test_list[i:j]
y = []
for j in x:
y.extend(j)
res = max (y)
print ( "The maximum element in row range ? : " + str (res))
|
Output
The original list is : [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]]
The maximum element in row range ? : 10
Method #4: using list comprehension
step-by-step approach to implement
- Define a list of lists with some values.
- Define a list of lists with some values.
- Use a list comprehension to create a new list containing the maximum element of each row in the specified range. This can be done using the max() function on each sublist in the range
- Find the maximum element of the max_values list using the max() function
- Print the result to the console
Python3
test_list = [[ 4 , 3 , 6 ], [ 9 , 1 , 3 ], [ 4 , 5 , 2 ], [ 9 , 10 , 3 ], [ 5 , 9 , 12 ], [ 3 , 14 , 2 ]]
i, j = 2 , 4
max_values = [ max (row) for row in test_list[i:j]]
result = max (max_values)
print ( "The maximum element in row range is:" , result)
|
Output
The maximum element in row range is: 10
The time complexity of this approach is O((j-i)*n), where n is the length of each row. This is because the code needs to iterate through all the rows within the specified range (j-i) and find the maximum element of each row, which takes O(n) time. The max() function takes O(n) time to find the maximum element of a list.
The auxiliary space complexity of this approach is O(j-i), which is the size of the max_values list. This is because the code creates a new list containing the maximum element of each row in the specified range using a list comprehension. The max_values list stores the maximum element of each row in the specified range. The size of this list is equal to the number of rows within the specified range, which is j-i.
Similar Reads
Python - Row with Maximum Record Element
Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read
Python - Ranged Maximum Element in String List
Sometimes, while working with Python data, we can have a problem in which we have data in form of String List and we require to find the maximum element in that data, but that also in a certain range of indices. This is quite peculiar problem but can have application in data domains. Let's discuss c
5 min read
Python | Keys with Maximum value
In Python, dictionaries are used to store data in key-value pairs and our task is to find the key or keys that have the highest value in a dictionary. For example, in a dictionary that stores the scores of students, you might want to know which student has the highest score. Different methods to ide
4 min read
Python - Maximum Sum Record
Sometimes, while working with data, we might have a problem in which we need to find maximum sum between available pairs in list. This can be application to many problems in mathematics domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using max() + list comprehensi
3 min read
Python | Maximum Difference in String
Sometimes, we might have a problem in which we require to get the maximum difference of 2 numbers from Strings but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Letâs discuss certain ways in which this problem can be solved. Met
4 min read
Python - Maximum of String Integer list
Sometimes, while working with data, we can have a problem in which we receive a series of lists with data in string format, which we wish to find the max of each string list integer. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + int() This is the brute forc
4 min read
Python - Row with Minimum Sum in Matrix
We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but having shorthands to perform the same are always helpful as this kind of problem can come in web development. Method #1 : Using reduce() + lambda The
4 min read
Python - Sort by range inclusion
Given a range, sort tuple Matrix by total range covered in a given range. [Considering tuples which completely lie within range]. Input : test_list = [[(1, 5), (6, 10), (10, 15)], [(3, 16)], [(2, 4), (6, 8), (9, 14)], [(1, 3), (9, 13)]], i, j = 2, 15 Output : [[(3, 16)], [(1, 5), (6, 10), (10, 15)],
4 min read
Python - Row with Maximum Product
We can have an application for finding the lists with the maximum value and print it. This seems quite an easy task and may also be easy to code, but having shorthands to perform the same are always helpful as this kind of problem can come in web development. Method #1 : Using reduce() + lambda The
4 min read
Key with Maximum Unique Values - Python
The task involves finding the key whose list contains the most unique values in a dictionary. Each list is converted to a set to remove duplicates and the key with the longest set is identified. For example, in d = {"A": [1, 2, 2], "B": [3, 4, 5, 3], "C": [6, 7, 7, 8]}, key "C" has the most unique v
3 min read