Find the maximum repeating number in O(n) time and O(1) extra space in Python

Finding the maximum repeating number in an array with constraints of O(n) time and O(1) extra space requires a clever approach. Given an array of size n with elements in range 0 to k-1 (where k ? n), we can modify the original array to store frequency information.

The key insight is to use the array indices as counters by adding k to the element at index A[i] % k for each element A[i].

Algorithm Steps

  • For each element in the array, increment the value at index A[i] % k by k

  • After processing, the element with the highest value indicates the most frequent number

  • Return the index of the maximum element as the most repeating number

How It Works

The algorithm uses the fact that A[i] % k gives the original value even after adding multiples of k. By adding k to A[A[i] % k], we're essentially counting occurrences at that index position.

Maximum Repeating Number Algorithm Visualization Original Array: 3 4 4 6 4 Process After Processing: 11 12 10 19 28 Index: 0 1 2 3 4 Maximum value at index 4 Therefore, 4 is most frequent

Implementation

def get_max_repeating(A, k):
    n = len(A)
    
    # Step 1: Increase value at A[A[i]%k] by k
    for i in range(n):
        A[A[i] % k] += k
    
    # Step 2: Find the maximum value and its index
    max_val = A[0]
    result = 0
    
    for i in range(1, n):
        if A[i] > max_val:
            max_val = A[i]
            result = i
    
    # Step 3: Restore original array (optional)
    for i in range(n):
        A[i] = A[i] % k
    
    return result

# Test the function
A = [3, 4, 4, 6, 4, 5, 2, 8]
k = 8
result = get_max_repeating(A, k)
print(f"Maximum repeating number: {result}")
Maximum repeating number: 4

Step-by-Step Execution

def get_max_repeating_verbose(A, k):
    n = len(A)
    print(f"Original array: {A}")
    print(f"k = {k}")
    
    # Step 1: Process each element
    for i in range(n):
        index = A[i] % k
        A[index] += k
        print(f"Processing A[{i}] = {A[i] - k}, adding k to A[{index}]")
        print(f"Array after step {i+1}: {A}")
    
    # Step 2: Find maximum
    max_val = A[0]
    result = 0
    
    for i in range(1, n):
        if A[i] > max_val:
            max_val = A[i]
            result = i
    
    print(f"\nMaximum value {max_val} found at index {result}")
    return result

# Example
A = [3, 4, 4, 6, 4, 5, 2, 8]
k = 8
result = get_max_repeating_verbose(A.copy(), k)
Original array: [3, 4, 4, 6, 4, 5, 2, 8]
k = 8
Processing A[0] = 3, adding k to A[3]
Array after step 1: [3, 4, 4, 14, 4, 5, 2, 8]
Processing A[1] = 4, adding k to A[4]
Array after step 2: [3, 4, 4, 14, 12, 5, 2, 8]
Processing A[2] = 4, adding k to A[4]
Array after step 3: [3, 4, 4, 14, 20, 5, 2, 8]
Processing A[3] = 6, adding k to A[6]
Array after step 4: [3, 4, 4, 14, 20, 5, 10, 8]
Processing A[4] = 4, adding k to A[4]
Array after step 5: [3, 4, 4, 14, 28, 5, 10, 8]
Processing A[5] = 5, adding k to A[5]
Array after step 6: [3, 4, 4, 14, 28, 13, 10, 8]
Processing A[6] = 2, adding k to A[2]
Array after step 7: [3, 4, 12, 14, 28, 13, 10, 8]
Processing A[7] = 0, adding k to A[0]
Array after step 8: [11, 4, 12, 14, 28, 13, 10, 8]

Maximum value 28 found at index 4

Key Points

  • Time Complexity: O(n) − we traverse the array twice

  • Space Complexity: O(1) − we modify the input array in-place

  • The algorithm works because (x + k) % k = x % k for any integer x

  • After adding k multiple times, the highest value indicates the most frequent element

Conclusion

This algorithm efficiently finds the maximum repeating number by using the input array itself as a frequency counter. The modulo operation ensures we can retrieve original values while the additions track occurrence counts.

---
Updated on: 2026-03-25T09:32:17+05:30

664 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements