Python Program to find Jumbo GCD Subarray


Jumbo GCD can be referred to find the Greatest Common Divisor (GCD) of a subarray with the maximum length of an array. The GCD is a set of positive integer numbers that divides all the numbers without having a remainder. We can find out the jumbo GCD subarray using two different approaches such as Brute force and Optimized approach using prefix sums. The solution of the jumbo GCD subarray is demonstrated in this article with relevant examples. The Brute force approach can be used to check all possible subarrays of the considering array and to calculate the GCD of an individual subarray.

Example 1: Finding the jumbo GCD subarray using the Brute force approach.

To solve a complex problem, we can use the Brute force approach. It is a straightforward way to achieve a possible solution to a given problem. Regarding the Jumbo GCD subarray problem.

Code Explanation and Design Steps for example 1

Step 1: Open Jupyter Notebook in the Anaconda prompt and start writing the code in its cell.

Step 2: Import the ‘array’ module.

Step 3: Use the ‘gcd ()’ function to calculate the GCD value of two numbers using the Euclidean algorithm. Here, we are considering two numbers such as a and b, if b=0 then GCD is an otherwise, GCD of a and b is the same as b and the remainder of a%b.

Step 4: The function ‘jumbo_gcd_brute_force’ uses the Brute force approach to search the jumbo GCD subarray.

Step 5: Initialize a variable ‘result’ and set this variable to zero. The GCD value can be stored in the ‘result’ variable.

Step 6: Indexing starts from index o to index n-1, where, n is the length of the array. It iterates all possible positions of the subarray from index zero to index n-1.

Step 7: Check whether the GCD of the latest subarray must be greater than the current result then update the ‘result’ variable with the new max. GCD.

Step 8: Finally, Check the final value of the GCD of the subarray along with the given array in ‘result’.

Code for Jumbo Brute force approach

Example

import array
def gcd(a, b):
    # Function to calculate the GCD of two numbers
    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
 # Example usage:
arr = [8, 12, 6, 4, 10,14]
print("Array:", arr)
print("Brute Force approach - Jumbo GCD subarray:", jumbo_gcd_brute_force(arr))

Output

Array: [8, 12, 6, 4, 10, 14]
Brute Force approach - Jumbo GCD subarray: 14

The Brute force approach takes each and every possible combination of subarrays. It calculates the GCD of individual one and finds the guaranteed maximum GCD subarray.

Example 2: Finding the jumbo GCD subarray using an Optimized approach.

To solve a jumbo GCD subarray problem, we can use another approach, which is called the Optimized approach. The aim of this approach is to reduce the time complexity as compared to the Brute force approach by using the properties of the GCD.

Code Explanation and Design Steps for example 2

Step 1: Open Jupyter Notebook in the Anaconda prompt and start writing the code in its cell.

Step 2: Import the ‘array’ module.

Step 3: Use ‘gcd ()’ function to calculate the GCD value of two numbers using the Euclidean algorithm. Here, we are considering two numbers such as a and b, if b=0 then GCD is an otherwise, GCD of a and b is the same as b and the remainder of a%b.

Step 4: The function ‘jumbo_gcd_optimized’ uses the optimized approach to find the jumbo GCD subarray.

Step 5: Initialize a variable ‘result’ and set this variable to zero. The GCD value can be stored in the ‘result’ variable.

Step 6: Indexing starts from index o to index n-1, where, n is the length of the array. It iterates all possible positions of the subarray from index zero to index n-1.

Step 7: Repeat step 6, iterating over the array in opposite sequence/reverse order which starts from index n-1 to index zero.

Step 8: Calculate the GCD to the current element with its ‘result’ and update it accordingly.

Step 9: Check whether the GCD of the latest subarray must be greater than the current result then update the ‘result’ variable with the new max. GCD.

Step 10: Finally, Check the final value of GCD of the subarray along with the given array in ‘result’.

Code for Optimized approach

Example

import array
def gcd(a, b):
    # Function is used to calculate the GCD of two numbers
    while b:
        a, b = b, a % b
    return a

def jumbo_gcd_optimized(arr):
    n = len(arr)
    prefix_gcd = [0] * n
    suffix_gcd = [0] * n
    prefix_gcd[0] = arr[0]
 # Forward iteration:
   
    for i in range(1, n):
        prefix_gcd[i] = gcd(prefix_gcd[i - 1], arr[i])
    suffix_gcd[n - 1] = arr[n - 1]
# Backward iteration:
    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
# Example usage:
arr = [8, 12, 6, 4, 10,14]
print("Array:", arr)
print("Optimized approach - Jumbo GCD subarray:", jumbo_gcd_optimized(arr))

Output

Array: [8, 12, 6, 4, 10, 14]
Optimized approach - Jumbo GCD subarray: 2

The optimized approach calculates prefix and suffix GCD arrays and stores the GCDs of both prefixes and suffixes of the real array accordingly. It iterates over the array by excluding the Ist and last elements and then calculates the max. GCD by taking both prefix and suffix GCD as well. This approach is more efficient for larger arrays.

Conclusion

In the jumbo GCD subarray article, using two different approaches with examples, these methods illustrate how to find the jumbo GCD value of subarray. The first method computes the value of a given array to obtain the value of the GCD subarray and the second method has the same objective to get the result but the style is different. The time complexity of the Brute force approach is O (n^3) whereas the optimized approach has the time complexity of O(n). As a result, the optimized approach is more efficient than the Brute force approach for larger arrays. We can consider a potential application of jumbo GCD in specific scenarios such as array processing, signal processing, cryptography, and so on.

Updated on: 28-Aug-2023

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements