Friday, September 5, 2025
HomeLanguagesPython – Row with Minimum difference in extreme values

Python – Row with Minimum difference in extreme values

Given a Matrix, extract the rows with a minimum difference in extreme values.

Examples:

Input : test_list = [[4, 10, 18], [5, 0], [1, 4, 6], [19, 2]] 
Output : [[1, 4, 6], [5, 0]] 
Explanation : 6 – 1 = 5, 5 – 0 = 5, is minimum difference between extreme values.

Input : test_list = [[4, 10, 18], [5, 0], [2, 4, 6], [19, 2]] 
Output : [[2, 4, 6]] 
Explanation : 6 – 2 = 4, is min diff.

Method #1 : Using list comprehension + min()

In this, we compute minimum difference between extreme values, and then use list comprehension to get particular row with that value difference between extreme values.

Python3




# Python3 code to demonstrate working of
# Matrix Minimum difference in extreme values row
# Using min() + list comprehension
 
# initializing list
test_list = [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting min value
min_val = min([max(sub) - min(sub) for sub in test_list])
 
# using list comprehension to filter
res = [sub for sub in test_list if max(sub) - min(sub) == min_val]
 
# printing result
print("Rows with Minimum difference in extreme values : " + str(res))


Output:

The original list is : [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]] Rows with Minimum difference in extreme values : [[1, 4, 6]]

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  list comprehension + min() performs n number of operations.
Auxiliary Space: O(n), extra space of size n is required

Method #2 : Using min() + list comprehension + filter() + lambda

In this, we perform task of filtering comparing with minimum value using filter() + lambda.

Python3




# Python3 code to demonstrate working of
# Matrix Minimum difference in extreme values row
# Using min() + list comprehension + filter() + lambda
 
# initializing list
test_list = [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting min value
min_val = min([max(sub) - min(sub) for sub in test_list])
 
# using filter() + lambda to filter
res = list(filter(lambda sub : max(sub) - min(sub) == min_val, test_list))
 
# printing result
print("Rows with Minimum difference in extreme values : " + str(res))


Output:

The original list is : [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]] Rows with Minimum difference in extreme values : [[1, 4, 6]]

Method #3: Using loops

Step-by-step approach:

  • Initialize a variable ‘min_diff’ to a very large value.
  • Initialize an empty list ‘rows’.
  • Loop through each row ‘sub’ in ‘test_list’.
    • Calculate the difference between the maximum and minimum value in ‘sub’.
    • If the difference is less than ‘min_diff’, update ‘min_diff’ to this value.
    • Clear the ‘rows’ list.

Python3




# initializing list
test_list = [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using loops to find the rows with minimum difference in extreme values
min_diff = float('inf')
rows = []
for sub in test_list:
    diff = max(sub) - min(sub)
    if diff < min_diff:
        min_diff = diff
        rows.clear()
    if diff == min_diff:
        rows.append(sub)
 
# printing result
print("Rows with Minimum difference in extreme values : " + str(rows))


Output

The original list is : [[4, 10, 18], [5, 3, 10], [1, 4, 6], [19, 2]]
Rows with Minimum difference in extreme values : [[1, 4, 6]]

Time complexity: O(nm), where n is the number of rows and m is the length of the longest row.
Auxiliary space: O(k), where k is the number of rows with the minimum difference in extreme values.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS