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 | Maximize Record list
Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension +
6 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 | Index Maximum among Tuples
Sometimes, while working with records, we might have a common problem of maximizing contents of one tuple with corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Letâs discuss certain ways in which this task can be performed. Metho
6 min read
Python - Maximum frequency in Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to find the maximum frequency element in the tuple. Tuple, being quite a popular container, this type of problem is common across the web development domain. Let's discuss certain ways in which this task can be perfo
5 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