Python - Maximum element in consecutive subsets
Last Updated :
12 Jul, 2025
We are given a list of numbers and a fixed integer k. The task is to determine the maximum number in every consecutive block of k elements. For instance, consider the list [1,3,2,5,4,6,8,7] with k=3. This list can be divided into consecutive subsets: the first subset is [1,3,2], the next is [3,2,5] followed by [2,5,4] then [5,4,6], next [4,6,8], and finally [6,8,7]. The maximum values from these subsets are 3, 5, 5, 6, 8, and 8 respectively, yielding the final output [3,5,5,6,8,8].
Using a Loop
A simple loop can be used to iterate through each consecutive subset find the maximum element within each subset and track overall maximum. This method provides a straightforward approach to finding the largest element in consecutive groups.
Python
a = [1, 3, 2, 5, 4, 6, 8, 7]
k = 3
maxi = []
for i in range(len(a) - k + 1):
# Slice the array for the current window
subset = a[i:i + k]
# Find and append the maximum of the subset
maxi.append(max(subset))
print(maxi)
Explanation:
- Loop iterates over list "a" slicing array into consecutive subsets of size "k" and finding the maximum element in each subset.
- Maximum values from each subset are stored in the "maxi" list which is then printed as result.
Using List Comprehension
List comprehension allows creating a list of maximum elements from consecutive subsets in a concise way by iterating over the list and applying a condition. It replaces loop with a single line of code to achieve same result.
Python
a = [1, 3, 2, 5, 4, 6, 8, 7]
k = 3
# List comprehension to find the maximum element in each consecutive subset of size k
maxi = [max(a[i:i + k]) for i in range(len(a) - k + 1)]
print(maxi)
Explanation:
- List comprehension iterates over the list "a" slicing it into consecutive subsets of size "k" and finding maximum value in each subset.
- Maximum values from each subset are stored in "maxi" list which is then printed as result.
Using Sliding Window Approach
Sliding window approach efficiently calculates the maximum of consecutive subsets by iterating through the list while maintaining a window of fixed size. It reduces redundant calculations by updating window incrementally as it slides through the list.
Python
a = [1, 3, 2, 5, 4, 6, 8, 7]
k = 3
maxi = []
for i in range(len(a) - k + 1):
# Find max of the current subset
maxi.append(max(a[i:i + k]))
print(maxi)
Explanation:
- Loop iterates through the list "a", slicing it into consecutive subsets of size "k" and finding the maximum element in each subset.
- Maximum elements of each subset are collected in the "maxi" list, which is then printed as the result.
Similar Reads
Python | Consecutive Subsets Minimum Some of the classical problems in programming domain comes from different categories and one among them is finding the minimum of subsets. This particular problem is also common when we need to accumulate the minimum and store consecutive group minimum. Letâs try different approaches to this problem
4 min read
Python - Consecutive elements maximum frequencies Sometimes, while working with Python lists we can have a problem in which we need to extract the maximum frequencies of consecutive elements. This kind of problem can occur as application in many domains such as day-day programming and competitive programming. Let's discuss certain ways in which thi
5 min read
Python - Maximum length consecutive positive elements Sometimes, while working with Python lists, we can have a problem in which we monitor a sequence and we need to find what was the maximum length when just positive elements occur. This kind of problem can have applications in data domains. Let's discuss certain ways in which this task can be perform
4 min read
Python - K consecutive Maximum 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 o
4 min read
Python | Retain K consecutive elements Sometimes while working with data, we can have a problem in which we need to select some of the elements that occur K times consecutively. This problem can occur in many domains. Let's discuss certain ways in which this problem can be solved. Method #1 : Using groupby() + list comprehension This t
8 min read
Python - Filter consecutive elements Tuples Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)] Output : [(3, 4, 5, 6)] Explanation : Only 1 tuple adheres to condition. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6,
5 min read