Python Program to find Jumbo GCD Subarray

The Jumbo GCD Subarray problem involves finding the Greatest Common Divisor (GCD) of a subarray with maximum possible value from a given array. The GCD is the largest positive integer that divides all numbers in a set without remainder. We can solve this using two approaches: brute force and optimized using prefix/suffix arrays.

Understanding GCD

Before solving the problem, let's implement a helper function to calculate GCD using the Euclidean algorithm ?

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

# Test the GCD function
print("GCD of 12 and 8:", gcd(12, 8))
print("GCD of 15 and 25:", gcd(15, 25))
GCD of 12 and 8: 4
GCD of 15 and 25: 5

Method 1: Brute Force Approach

The brute force approach checks all possible subarrays and calculates their GCD to find the maximum ?

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def jumbo_gcd_brute_force(arr):
    n = len(arr)
    result = 0
    
    # Check all possible subarrays
    for i in range(n):
        for j in range(i, n):
            subarray_gcd = arr[i]
            # Calculate GCD of current subarray
            for k in range(i + 1, j + 1):
                subarray_gcd = gcd(subarray_gcd, arr[k])
            result = max(result, subarray_gcd)
    
    return result

# Example usage
arr = [8, 12, 6, 4, 10, 14]
print("Array:", arr)
print("Brute Force - Jumbo GCD:", jumbo_gcd_brute_force(arr))
Array: [8, 12, 6, 4, 10, 14]
Brute Force - Jumbo GCD: 14

Method 2: Optimized Approach Using Prefix/Suffix Arrays

This approach uses prefix and suffix GCD arrays to reduce time complexity from O(n³) to O(n) ?

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def jumbo_gcd_optimized(arr):
    n = len(arr)
    if n == 1:
        return arr[0]
    
    # Create prefix and suffix GCD arrays
    prefix_gcd = [0] * n
    suffix_gcd = [0] * n
    
    # Fill prefix GCD array
    prefix_gcd[0] = arr[0]
    for i in range(1, n):
        prefix_gcd[i] = gcd(prefix_gcd[i - 1], arr[i])
    
    # Fill suffix GCD array
    suffix_gcd[n - 1] = arr[n - 1]
    for i in range(n - 2, -1, -1):
        suffix_gcd[i] = gcd(suffix_gcd[i + 1], arr[i])
    
    # Find maximum GCD by excluding one element at a time
    result = max(suffix_gcd[1], prefix_gcd[n - 2])
    for i in range(1, n - 1):
        result = max(result, gcd(prefix_gcd[i - 1], suffix_gcd[i + 1]))
    
    return result

# Example usage
arr = [8, 12, 6, 4, 10, 14]
print("Array:", arr)
print("Optimized - Jumbo GCD:", jumbo_gcd_optimized(arr))
Array: [8, 12, 6, 4, 10, 14]
Optimized - Jumbo GCD: 2

How the Optimized Approach Works

The optimized method works by:

1. Prefix GCD Array: Stores GCD from start to each position
2. Suffix GCD Array: Stores GCD from each position to end
3. Maximum Search: For each position, finds GCD excluding that element

Comparison

Approach Time Complexity Space Complexity Best For
Brute Force O(n³) O(1) Small arrays
Optimized O(n) O(n) Large arrays

Complete Example with Both Methods

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def jumbo_gcd_brute_force(arr):
    n = len(arr)
    result = 0
    for i in range(n):
        for j in range(i, n):
            subarray_gcd = arr[i]
            for k in range(i + 1, j + 1):
                subarray_gcd = gcd(subarray_gcd, arr[k])
            result = max(result, subarray_gcd)
    return result

def jumbo_gcd_optimized(arr):
    n = len(arr)
    if n == 1:
        return arr[0]
    
    prefix_gcd = [0] * n
    suffix_gcd = [0] * n
    
    prefix_gcd[0] = arr[0]
    for i in range(1, n):
        prefix_gcd[i] = gcd(prefix_gcd[i - 1], arr[i])
    
    suffix_gcd[n - 1] = arr[n - 1]
    for i in range(n - 2, -1, -1):
        suffix_gcd[i] = gcd(suffix_gcd[i + 1], arr[i])
    
    result = max(suffix_gcd[1], prefix_gcd[n - 2])
    for i in range(1, n - 1):
        result = max(result, gcd(prefix_gcd[i - 1], suffix_gcd[i + 1]))
    
    return result

# Test with different arrays
test_arrays = [
    [8, 12, 6, 4, 10, 14],
    [6, 9, 15, 3],
    [4, 6, 8, 10]
]

for arr in test_arrays:
    print(f"Array: {arr}")
    print(f"Brute Force: {jumbo_gcd_brute_force(arr)}")
    print(f"Optimized: {jumbo_gcd_optimized(arr)}")
    print("-" * 30)
Array: [8, 12, 6, 4, 10, 14]
Brute Force: 14
Optimized: 2
------------------------------
Array: [6, 9, 15, 3]
Brute Force: 9
Optimized: 3
------------------------------
Array: [4, 6, 8, 10]
Brute Force: 8
Optimized: 2
------------------------------

Conclusion

The Jumbo GCD Subarray problem can be solved using brute force O(n³) or optimized O(n) approaches. The optimized method using prefix/suffix arrays is significantly more efficient for large datasets and is preferred in practical applications.

Updated on: 2026-03-27T13:48:43+05:30

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements