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
# Python3 code to demonstrate working of
# Maximum in Rows Range
# Using max() + slicing
# initializing list
test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2],
[9, 10, 3], [5, 9, 12], [3, 14, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 2, 4
res = 0
for idx in range(i, j):
# getting max in range
res = max(max(test_list[idx]), res)
# printing result
print("The maximum element in row range ? : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Maximum in Rows Range
# Using max() + slicing + list comprehension
# initializing list
test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2],
[9, 10, 3], [5, 9, 12], [3, 14, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 2, 4
# getting max of maximum of sub lists
res = max([max(test_list[idx]) for idx in range(i, j)])
# printing result
print("The maximum element in row range ? : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Maximum in Rows Range
# initializing list
test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2],
[9, 10, 3], [5, 9, 12], [3, 14, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 2, 4
res = 0
x=test_list[i:j]
y=[]
for j in x:
y.extend(j)
res=max(y)
# printing result
print("The maximum element in row range ? : " + str(res))
OutputThe 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
# initialize a list of lists with some values
test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]]
# specify the range of rows to consider using variables i and j
i, j = 2, 4
# create a new list containing the maximum element of each row in the specified range using a list comprehension
max_values = [max(row) for row in test_list[i:j]]
# find the maximum element of the max_values list using the max function
result = max(max_values)
# print the maximum element in the specified range of rows to the console
print("The maximum element in row range is:", result)
OutputThe 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
4 min read
Keys with Maximum value - Python 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 - 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
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read