Check if at least half array is reducible to zero by performing some operation in Python

Given an array of positive integers and a positive integer m, we need to determine if at least half of the array elements can be reduced to zero through a specific operation. In each iteration, we decrease some elements by 1 and increase the remaining elements by m.

The key insight is that elements with the same remainder when divided by (m + 1) will follow the same pattern and can potentially reach zero together.

Algorithm

To solve this problem, we follow these steps:

  • Create a frequency array to count elements by their remainder when divided by (m + 1)
  • Check if any remainder group has at least half the total elements
  • If such a group exists, return True; otherwise, return False

Example

Let's implement the solution with a clear example:

def solve(input_list, m):
    frequency_list = [0] * (m + 1)
    
    # Count frequency of remainders when divided by (m + 1)
    for num in input_list:
        remainder = num % (m + 1)
        frequency_list[remainder] += 1
    
    # Check if any remainder group has at least half elements
    required_count = len(input_list) / 2
    for count in frequency_list:
        if count >= required_count:
            return True
    
    return False

# Test the function
input_list = [10, 18, 35, 5, 12]
m = 4
result = solve(input_list, m)
print(f"Input: {input_list}, m = {m}")
print(f"Output: {result}")
Input: [10, 18, 35, 5, 12], m = 4
Output: True

How It Works

Let's trace through the example to understand the logic:

def solve_with_trace(input_list, m):
    frequency_list = [0] * (m + 1)
    
    print(f"Input array: {input_list}")
    print(f"m = {m}, so we use modulo {m + 1}")
    
    # Calculate remainders and frequencies
    remainders = []
    for num in input_list:
        remainder = num % (m + 1)
        remainders.append(remainder)
        frequency_list[remainder] += 1
    
    print(f"Remainders: {remainders}")
    print(f"Frequency count: {frequency_list}")
    
    required_count = len(input_list) / 2
    print(f"Required count (at least half): {required_count}")
    
    # Check each remainder group
    for i, count in enumerate(frequency_list):
        if count >= required_count:
            print(f"Remainder {i} has {count} elements (>= {required_count})")
            return True
    
    return False

# Test with trace
input_list = [10, 18, 35, 5, 12]
result = solve_with_trace(input_list, 4)
print(f"Result: {result}")
Input array: [10, 18, 35, 5, 12]
m = 4, so we use modulo 5
Remainders: [0, 3, 0, 0, 2]
Frequency count: [3, 0, 1, 1, 0]
Required count (at least half): 2.5
Remainder 0 has 3 elements (>= 2.5)
Result: True

Key Points

  • Elements with remainder 0 when divided by (m + 1) can be reduced to zero by only applying the decrease operation
  • The algorithm groups elements by their remainder modulo (m + 1)
  • If any group contains at least half the elements, those elements can reach zero together
  • Time complexity: O(n), where n is the length of the input array
  • Space complexity: O(m) for the frequency array

Conclusion

This solution efficiently determines if at least half of an array can be reduced to zero by grouping elements with the same remainder modulo (m + 1). The key insight is that elements in the same remainder group follow identical reduction patterns.

Updated on: 2026-03-25T14:58:04+05:30

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements