Program to find a sub-list of size at least 2 whose sum is multiple of k in Python

Suppose we have a list of non-negative numbers called nums and another positive value k. We have to check whether there is any sublist of length at least 2 whose sum is multiple of k or not.

So, if the input is like nums = [12, 6, 3, 4] k = 5, then the output will be True, as the sublist is [12, 3] sums to 15 which is divisible by 5.

Algorithm

To solve this, we will follow these steps ?

  • sum := 0
  • m := a new map
  • m[0] := -1
  • for i in range 0 to size of nums, do
    • sum := sum + nums[i]
    • sum := sum mod k
    • if sum is present in m, then
      • if i - m[sum] >= 2, then
        • return True
    • otherwise,
      • m[sum] := i
  • return False

How It Works

The algorithm uses the concept of cumulative sum modulo k. If two cumulative sums have the same remainder when divided by k, then the sublist between them has a sum that is divisible by k.

Example

Let us see the following implementation to get better understanding ?

class Solution:
    def solve(self, nums, k):
        sum_val = 0
        m = {}
        m[0] = -1
        for i in range(0, len(nums)):
            sum_val += nums[i]
            sum_val %= k
            if sum_val in m:
                if i - m[sum_val] >= 2:
                    return True
            else:
                m[sum_val] = i
        return False

ob = Solution()
nums = [12, 6, 3, 4]
k = 5
print(ob.solve(nums, k))
True

Step-by-Step Execution

Let's trace through the example with nums = [12, 6, 3, 4] and k = 5 ?

def solve_with_trace(nums, k):
    sum_val = 0
    m = {}
    m[0] = -1
    print(f"Initial: sum_val = {sum_val}, m = {m}")
    
    for i in range(len(nums)):
        sum_val += nums[i]
        sum_val %= k
        print(f"i = {i}, nums[i] = {nums[i]}, sum_val % k = {sum_val}")
        
        if sum_val in m:
            if i - m[sum_val] >= 2:
                print(f"Found: sublist from index {m[sum_val] + 1} to {i}")
                return True
        else:
            m[sum_val] = i
            print(f"Updated m: {m}")
    
    return False

nums = [12, 6, 3, 4]
k = 5
result = solve_with_trace(nums, k)
print(f"Result: {result}")
Initial: sum_val = 0, m = {0: -1}
i = 0, nums[i] = 12, sum_val % k = 2
Updated m: {0: -1, 2: 0}
i = 1, nums[i] = 6, sum_val % k = 3
Updated m: {0: -1, 2: 0, 3: 1}
i = 2, nums[i] = 3, sum_val % k = 1
Updated m: {0: -1, 2: 0, 3: 1, 1: 2}
i = 3, nums[i] = 4, sum_val % k = 0
Found: sublist from index 0 to 3
Result: True

Alternative Implementation

Here's a more straightforward function-based approach ?

def has_sublist_sum_multiple_of_k(nums, k):
    """
    Check if there exists a sublist of length >= 2 whose sum is divisible by k
    """
    cumulative_sum = 0
    remainder_map = {0: -1}  # remainder -> first index where this remainder occurred
    
    for i, num in enumerate(nums):
        cumulative_sum += num
        remainder = cumulative_sum % k
        
        if remainder in remainder_map:
            # If we've seen this remainder before and sublist length >= 2
            if i - remainder_map[remainder] >= 2:
                return True
        else:
            remainder_map[remainder] = i
    
    return False

# Test cases
test_cases = [
    ([12, 6, 3, 4], 5),
    ([23, 2, 4, 6, 7], 6),
    ([23, 2, 4, 6, 6], 7),
    ([1, 0], 1)
]

for nums, k in test_cases:
    result = has_sublist_sum_multiple_of_k(nums, k)
    print(f"nums = {nums}, k = {k} ? {result}")
nums = [12, 6, 3, 4], k = 5 ? True
nums = [23, 2, 4, 6, 7], k = 6 ? True
nums = [23, 2, 4, 6, 6], k = 7 ? False
nums = [1, 0], k = 1 ? True

Conclusion

This algorithm efficiently finds sublists with sums divisible by k using cumulative sum and modular arithmetic. The time complexity is O(n) and space complexity is O(min(n, k)) where n is the length of the input list.

Updated on: 2026-03-25T12:28:38+05:30

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements