Open In App

Python – Maximum in Row Range

Last Updated : 08 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


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




# 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))


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




# 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))


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




# 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)


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads