Open In App

Python | Consecutive Pair Minimums

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python list, one can have a problem in which one needs to find perform the minimum of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using loop This is the brute force method to perform this particular task. In this, we just iterate the list till last element in skipped manner to get all the pair minimum in other list in iterative way. 

Python3




# Python3 code to demonstrate working of
# Consecutive Pair Minimums
# Using loop
 
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
 
# printing list
print("The original list : " + str(test_list))
 
# Consecutive Pair Minimums
# Using loop
res = []
for ele in range(0, len(test_list), 2):
    res.append(min(test_list[ele], test_list[ele + 1]))
 
# Printing result
print("Pair minimum of list : " + str(res))


Output : 

The original list : [4, 5, 8, 9, 10, 17]
Pair minimum of list : [4, 8, 10]

Time Complexity: O(n*n) where n is the number of elements in the string list. The loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.

  Method #2 : Using zip() + min() + list comprehension This task can also be performed using the combination of above functionalities. In this, we just iterate the list and the task of combining pairs is performed by zip(). Works only on Python2. 

Python




# Python code to demonstrate working of
# Consecutive Pair Minimums
# zip() + list comprehension + min()
 
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
 
# printing list
print("The original list : " + str(test_list))
 
# Consecutive Pair Minimums
# zip() + list comprehension + min()
res = [min(i, j) for i, j in zip(test_list, test_list[1:])[::2]]
 
# Printing result
print("Pair minimum of list : " + str(res))


Output : 

The original list : [4, 5, 8, 9, 10, 17]
Pair minimum of list : [4, 8, 10]

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3 : Using map() + min()
# This task can be performed using combination of above functions. In this, we use the map() to perform the task of combining the elements of list and min() to perform the task of finding the minimum.
 

Python3




# Python3 code to demonstrate working of
# Consecutive Pair Minimums
# map() + min()
   
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
   
# printing list
print("The original list : " + str(test_list))
   
# Consecutive Pair Minimums
# map() + min()
res = list(map(min, test_list[::2], test_list[1::2]))
   
# Printing result
print("Pair minimum of list : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [4, 5, 8, 9, 10, 17]
Pair minimum of list : [4, 8, 10]

Time complexity: o(n)

Auxiliary Space: o(n)

Method#4: Using Recursive method.

Algorithm:

  1. Define a function “consecutive_pair_min_recursive” that takes a list “lst” as input.
  2. If the length of the list “lst” is 0, return an empty list.
  3. If the length of the list “lst” is 1, return the list itself.
  4. Otherwise, find the minimum value of the first two elements in the list “lst” and store it in a new list.
  5. Recursively call the function “consecutive_pair_min_recursive” on the list “lst” starting from the third element (i.e., the index 2) and concatenate the result to the new list obtained in step 4.
  6. Return the new list obtained in step 5.
     

Python3




def consecutive_pair_min_recursive(lst):
    if len(lst) == 0:
        return []
    elif len(lst) == 1:
        return lst
    else:
        return [min(lst[0], lst[1])] + consecutive_pair_min_recursive(lst[2:])
 
 
 
 
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
 
# printing list
print("The original list : " + str(test_list))
 
# Finding consecutive pair minimums using recursion
res = consecutive_pair_min_recursive(test_list)
 
# Printing result
print("Pair minimum of list : " + str(res))
#this code contributed by tvsk


Output

The original list : [4, 5, 8, 9, 10, 17]
Pair minimum of list : [4, 8, 10]

Time complexity: The time complexity of the recursive method is O(n), where n is the number of elements in the input list. This is because the function visits each element in the list exactly once.

Auxiliary space: The auxiliary space complexity of the recursive method is O(n), where n is the number of elements in the input list. This is because the function creates a new list of size n to store the result, and the maximum number of function calls on the call stack is also proportional to n.



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads