Number of subarrays with GCD equal to 1

Given an array arr[], the task is to find the number of sub-arrays with GCD value equal to 1.

Examples:

Input: arr[] = {1, 1, 1}
Output: 6
All the subarrays of the given array
will have GCD equal to 1.



Input: arr[] = {2, 2, 2}
Output: 0

Approach: The key observation is that if the GCD of all the elements of the sub-array arr[l…r] is known then the GCD of all the elements of the sub-array arr[l…r+1] can be obtained by simply taking the GCD of the previous sub-array with arr[r + 1].
Thus, for every index i, keep iterating forward and compute the GCD from index i to j and check if its equal to 1.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the required count
int cntSubArr(int* arr, int n)
{
    // To store the final answer
    int ans = 0;
  
    for (int i = 0; i < n; i++) {
  
        // To store the GCD starting from
        // index 'i'
        int curr_gcd = 0;
  
        // Loop to find the gcd of each subarray
        // from arr[i] to arr[i...n-1]
        for (int j = i; j < n; j++) {
            curr_gcd = __gcd(curr_gcd, arr[j]);
  
            // Increment the count if curr_gcd = 1
            ans += (curr_gcd == 1);
        }
    }
  
    // Return the final answer
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 1, 1 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << cntSubArr(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
// Function to return the required count
static int cntSubArr(int []arr, int n)
{
    // To store the final answer
    int ans = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // To store the GCD starting from
        // index 'i'
        int curr_gcd = 0;
  
        // Loop to find the gcd of each subarray
        // from arr[i] to arr[i...n-1]
        for (int j = i; j < n; j++) 
        {
            curr_gcd = __gcd(curr_gcd, arr[j]);
  
            // Increment the count if curr_gcd = 1
            ans += (curr_gcd == 1) ? 1 : 0;
        }
    }
  
    // Return the final answer
    return ans;
}
  
static int __gcd(int a, int b) 
    if (b == 0
        return a; 
    return __gcd(b, a % b);     
}
  
// Driver code
public static void main(String []args) 
{
    int arr[] = { 1, 1, 1 };
    int n = arr.length;
  
    System.out.println(cntSubArr(arr, n));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
from math import gcd
  
# Function to return the required count 
def cntSubArr(arr, n) :
  
    # To store the final answer 
    ans = 0
  
    for i in range(n) :
  
        # To store the GCD starting from 
        # index 'i' 
        curr_gcd = 0
  
        # Loop to find the gcd of each subarray 
        # from arr[i] to arr[i...n-1] 
        for j in range(i, n) :
            curr_gcd = gcd(curr_gcd, arr[j]); 
  
            # Increment the count if curr_gcd = 1 
            ans += (curr_gcd == 1);
  
    # Return the final answer 
    return ans; 
  
# Driver code 
if __name__ == "__main__" :
  
    arr = [ 1, 1, 1 ]; 
    n = len(arr); 
  
    print(cntSubArr(arr, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
                      
class GFG 
{
  
// Function to return the required count
static int cntSubArr(int []arr, int n)
{
    // To store the final answer
    int ans = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // To store the GCD starting from
        // index 'i'
        int curr_gcd = 0;
  
        // Loop to find the gcd of each subarray
        // from arr[i] to arr[i...n-1]
        for (int j = i; j < n; j++) 
        {
            curr_gcd = __gcd(curr_gcd, arr[j]);
  
            // Increment the count if curr_gcd = 1
            ans += (curr_gcd == 1) ? 1 : 0;
        }
    }
  
    // Return the final answer
    return ans;
}
  
static int __gcd(int a, int b) 
    if (b == 0) 
        return a; 
    return __gcd(b, a % b);     
}
  
// Driver code
public static void Main(String []args) 
{
    int []arr = { 1, 1, 1 };
    int n = arr.Length;
  
    Console.WriteLine(cntSubArr(arr, n));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

6

Time Complexity: O(N2)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, Rajput-Ji