Find an integer X which is divisor of all except exactly one element in an array in Python

Given an array of numbers, we need to find an integer X that divides all elements except exactly one element. This problem assumes that the GCD of all elements is not 1, meaning there exists a common divisor greater than 1.

For example, if the input is [8, 16, 4, 24], the output will be 8 because 8 divides all elements (8, 16, 24) except one element (4).

Algorithm Approach

The solution uses prefix and suffix GCD arrays to efficiently find the GCD of all elements excluding each position ?

  • Prefix GCD: Store GCD from start to each position
  • Suffix GCD: Store GCD from each position to end
  • Check each position: Find GCD excluding current element and verify if current element is not divisible by it

Implementation

from math import gcd

def getDivisor(array):
    n = len(array)
    
    # Special case: single element
    if n == 1:
        return array[0] + 1
    
    # Create prefix and suffix GCD arrays
    prefix = [0] * n
    suffix = [0] * n
    
    # Fill prefix GCD array
    prefix[0] = array[0]
    for i in range(1, n):
        prefix[i] = gcd(array[i], prefix[i - 1])
    
    # Fill suffix GCD array
    suffix[n - 1] = array[n - 1]
    for i in range(n - 2, -1, -1):
        suffix[i] = gcd(suffix[i + 1], array[i])
    
    # Check each position
    for i in range(n):
        cur = 0
        if i == 0:
            cur = suffix[i + 1]
        elif i == n - 1:
            cur = prefix[i - 1]
        else:
            cur = gcd(prefix[i - 1], suffix[i + 1])
        
        # If current element is not divisible by cur, return cur
        if array[i] % cur != 0:
            return cur
    
    return 0

# Test the function
array = [8, 16, 4, 24]
result = getDivisor(array)
print(f"Array: {array}")
print(f"Divisor: {result}")
Array: [8, 16, 4, 24]
Divisor: 8

How It Works

Let's trace through the example [8, 16, 4, 24] ?

from math import gcd

def trace_algorithm(array):
    n = len(array)
    print(f"Input array: {array}")
    
    # Create prefix and suffix arrays
    prefix = [0] * n
    suffix = [0] * n
    
    # Fill prefix
    prefix[0] = array[0]
    for i in range(1, n):
        prefix[i] = gcd(array[i], prefix[i - 1])
    
    # Fill suffix
    suffix[n - 1] = array[n - 1]
    for i in range(n - 2, -1, -1):
        suffix[i] = gcd(suffix[i + 1], array[i])
    
    print(f"Prefix GCD: {prefix}")
    print(f"Suffix GCD: {suffix}")
    
    # Check each position
    for i in range(n):
        if i == 0:
            cur = suffix[i + 1]
        elif i == n - 1:
            cur = prefix[i - 1]
        else:
            cur = gcd(prefix[i - 1], suffix[i + 1])
        
        print(f"Position {i}: element={array[i]}, GCD excluding this={cur}, divisible? {array[i] % cur == 0}")
        
        if array[i] % cur != 0:
            return cur

# Trace the example
trace_algorithm([8, 16, 4, 24])
Input array: [8, 16, 4, 24]
Prefix GCD: [8, 8, 4, 4]
Suffix GCD: [4, 8, 4, 24]
Position 0: element=8, GCD excluding this=8, divisible? True
Position 1: element=16, GCD excluding this=8, divisible? True
Position 2: element=4, GCD excluding this=8, divisible? False

Key Points

  • Time Complexity: O(n log(max_element)) due to GCD operations
  • Space Complexity: O(n) for prefix and suffix arrays
  • The algorithm finds the first element that is not divisible by the GCD of remaining elements
  • Returns 0 if no such divisor exists

Conclusion

This algorithm efficiently finds a divisor of all elements except one using prefix and suffix GCD arrays. The key insight is computing the GCD of all elements excluding each position and checking divisibility.

Updated on: 2026-03-25T10:09:44+05:30

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements