5 Best Ways to Find a Sublist of Size At Least 2 Whose Sum Is Multiple of K in Python

Rate this post

💡 Problem Formulation: We are looking for algorithms that can find a continuous sublist within a larger list of integers where the sum of that sublist is a multiple of a given number k. The sublist must be of at least size 2. For example, given the list [1, 2, 3, 4, 5] and k = 3, a valid output could be the sublist [1, 2], since (1+2) = 3 is a multiple of k.

Method 1: Brute Force Approach

This method explicitly checks all possible sublists for the desired property. It is the most straightforward approach, iterating through each pair of indices in the list and checking if the sum of the elements between them is a multiple of k. The function specification usually includes the list and the value of k.

Here’s an example:

def find_sublist_brute_force(lst, k):
    n = len(lst)
    for start in range(n):
        for end in range(start + 1, n):
            if sum(lst[start:end+1]) % k == 0:
                return lst[start:end+1]
    return None

# Example usage
print(find_sublist_brute_force([1, 2, 3, 4, 5], 3))


[1, 2]

This method iterates over all possible start and end points for sublists and calculates the sum for each, checking if it’s divisible by k. If it finds such a sublist, it returns it immediately; otherwise, it returns None. This approach can be inefficient for large lists as its time complexity is O(n²), where n is the length of the list.

Method 2: Prefix Sum and Hashing

In this clever approach, we use a hash map to remember the sum modulo k of all prefixes of the list. Whenever two prefixes have the same remainder when divided by k, the elements lying between them sum to a multiple of k. This method works because of the ‘Pigeonhole Principle’ and is more efficient than the brute force approach.

Here’s an example:

def find_sublist_prefix_sum(lst, k):
    prefix_sum_mod_k = {0: -1}  # Initialize to handle the case where prefix_sum % k == 0
    prefix_sum = 0
    for i, num in enumerate(lst):
        prefix_sum += num
        remainder = prefix_sum % k
        if remainder in prefix_sum_mod_k and i - prefix_sum_mod_k[remainder] > 1:
            return lst[prefix_sum_mod_k[remainder]+1:i+1]
        if remainder not in prefix_sum_mod_k:
            prefix_sum_mod_k[remainder] = i
    return None

# Example usage
print(find_sublist_prefix_sum([1, 2, 3, 4, 5], 6))


[1, 2, 3]

This function keeps a running sum of elements and stores their remainders when divided by k. If a remainder repeats, it means a sublist adding to a multiple of k has been found. Unlike the brute force approach, this one runs in O(n) time, making it more scalable.

Method 3: Sliding Window Technique

The sliding window technique is useful when you can find the answer to a problem in a contiguous sequence of elements in an array. If k is known to be the sum of some subset, we can slide a window over the list to find sublists that add up to multiples of k. However, this method only works when k is fixed and the list contains only positive numbers.

Here’s an example:

def find_sublist_sliding_window(lst, k):
    window_sum = lst[0]
    start = 0

    for end in range(1, len(lst)):
        while window_sum > k and start = 1:
            return lst[start:end+1]
        window_sum += lst[end]

    return None

# Example usage
print(find_sublist_sliding_window([1, 2, 8, 4, 5], 10))


[2, 8]

This technique is useful when we have a running sum which we adjust by moving the start or end of the current window. Efficiency-wise it’s O(n) time complexity and thus scales well for large lists.

Method 4: Cumulative Sum with Early Stopping

Cumulative sum with early stopping is a variation of the brute force approach. It precomputes a cumulative sum list, reducing the time needed to calculate the sum of a range of elements. Then, it compares each possible pair of indices (i, j) where i < j, but unlike brute force, it stops early if the sum becomes greater than k.

Here’s an example:

def find_sublist_cumulative_sum(lst, k):
    cumulative_sum = [0]
    for num in lst:
        cumulative_sum.append(num + cumulative_sum[-1])

    for start in range(len(lst)):
        for end in range(start + 1, len(lst)):
            sublist_sum = cumulative_sum[end+1] - cumulative_sum[start]
            if sublist_sum % k == 0:
                return lst[start:end+1]
            if sublist_sum > k:  # Early stopping
    return None

# Example usage
print(find_sublist_cumulative_sum([1, 2, 3, 4, 5], 3))


[1, 2]

This method is an optimization over the brute-force approach which reduces the number of operations needed to calculate a range’s sum. It is more efficient but still not as fast as the prefix sum and hashing, with a worst-case time complexity of O(n²).

Bonus One-Liner Method 5: List Comprehension with Brute Force

This one-liner uses a combination of list comprehensions and the brute force method to find a sublist. While not the most efficient, it is a succinct way to write the code and makes use of Python’s expressive syntax.

Here’s an example:

find_sublist_one_liner = lambda lst, k: next((lst[i:j+1] for i in range(len(lst)) for j in range(i+1, len(lst)) if sum(lst[i:j+1]) % k == 0), None)

# Example usage
print(find_sublist_one_liner([1, 2, 3, 4, 5], 3))


[1, 2]

This one-liner defines a lambda function that uses a generator expression to iterate over all sublists, checking for the condition. It uses next() to return the first match or None if no sublist satisfies the condition. This method is the least efficient due to its O(n²) complexity but is included for its succinctness.


  • Method 1: Brute Force Approach. Easy to understand. Inefficient for large lists due to O(n²) complexity.
  • Method 2: Prefix Sum and Hashing. Efficient O(n) complexity. Utilizes hashing for quick lookups but might be harder to comprehend for beginners.
  • Method 3: Sliding Window Technique. Efficient for lists with positive integers. Not suitable for lists with negative numbers or when k is variable.
  • Method 4: Cumulative Sum with Early Stopping. Optimized brute force. Better than pure brute force but still O(n²) complexity in the worst case.
  • Method 5: List Comprehension with Brute Force. Offers brevity. Far less efficient and mostly useful for small lists or quick, dirty solutions.