Open In App

Python – K consecutive Maximum

Improve
Improve
Like Article
Like
Save
Share
Report

Given a List, find maximum of next K elements from each index.

Input : test_list = [4, 3, 9, 2, 6, 12, 4, 3, 2, 4, 5], K = 4 
Output : [9, 9, 9, 12, 12, 12, 4, 5] 
Explanation : Max of next 4 elements, (max(4, 3, 9, 2) = 9) 

Input : test_list = [4, 3, 9, 2, 6], K = 4 
Output : [9, 9] 
Explanation : Max of next 4 elements, (max(4, 3, 9, 2) = 9)

Method #1 : Using loop + max() + slicing

In this, we iterate for elements in loop, slicing till next K, and use max() to get maximum of them for current index.

Python3




# Python3 code to demonstrate working of
# K consecutive Maximum
# Using max() + loop + slicing
 
# initializing list
test_list = [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
res = []
for idx in range(len(test_list) - K + 1):
     
    # slice next K and compute Maximum
    res.append(max(test_list[idx : idx + K]))
 
# printing result
print("Next K Maximum List : " + str(res))


Output

The original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]

Time Complexity: O(n), where n is the length of the input list. 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”. 

Method #2 : Using list comprehension

This is yet another way to solve this, one-liner alternative to above method using list comprehension.

Python3




# Python3 code to demonstrate working of
# K consecutive Maximum
# Using list comprehension
 
# initializing list
test_list = [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# one-liner to solve problem
res = [max(test_list[idx : idx + K]) for idx in range(len(test_list) - K + 1)]
 
# printing result
print("Next K Maximum List : " + str(res))


Output

The original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]

Method 3: Using a deque

  • Initialize an empty deque to store the indices of elements in the current window.
  • Initialize a list res to store the K consecutive maximum values.
  • Loop through the input list test_list, starting at index 0.
  • In each iteration, add the current index to the right end of the deque.
  • If the length of the deque is greater than K, remove the leftmost index (i.e. the index from the previous window).
  • If the index is greater than or equal to K – 1:
    a. Append the maximum value in the current window (i.e. the value at the left end of the deque) to the res list.
    b. If the maximum value in the current window is equal to the value at the left end of the deque, pop the leftmost index from the deque.
  • Return the res list.

Python3




from collections import deque
 
# initializing list
test_list = [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# initialize variables
dq = deque()
res = []
 
# loop through input list
for i in range(len(test_list)):
    # add current index to deque
    dq.append(i)
     
    # remove leftmost index if necessary
    if len(dq) > K:
        dq.popleft()
     
    # add maximum value to res if necessary
    if i >= K - 1:
        window = [test_list[j] for j in dq]
        res.append(max(window))
        if dq[0] == i - K + 1:
            dq.popleft()
 
# printing result
print("Next K Maximum List : " + str(res))


Output

The original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(k), where k is the window size.



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