Number of subarrays with GCD equal to 1 in O(n) time
Last Updated :
09 Jan, 2023
Given an array arr[] of size N, the task is to find the maximum number of sub-arrays such that the GCD(Greatest Common Factor) of all elements of each sub-array is equal to 1 in O(n).
Examples:
Input: arr[] = {4, 2, 3, 0, 1, 8, 16, 1}.
Output: 3
Explanation: GCD of subarray {4, 2, 3} is 1, GCD of subarray {0, 1} is 1. GCD of {8, 16, 1} is 1, maximum number of subarrays is 3.
Input: arr[] = {9, 36, 8, 3, 7, 2, 13, 26, 5}
Output: 4
Explanation: GCD of subarray {9, 36, 8} is 1, GCD of subarray {3, 7} is 1, GCD of {2, 13} is 1, GCD of {26, 5} is 1, maximum number of subarrays is 4.
Approach: To solve the problem follow the below observations:
- GCD of any number with 0 is the number itself, hence, initially set GCD as 0.
- GCD of any two numbers will be 1 if they don’t have any common factor.
In the traversal:
- Keep on calculating the GCD of the current element with the GCD of the subarray till now.
- At any position, If the GCD of the current subarray becomes 1 then increase the count of the subarray by one and set the gcd to 0 so that we can calculate the maximum subarray.
- Return the number of subarrays.
Follow the given steps to solve the problem:
- Declare variables cnt and g and initialize them to 0.
- variable g denotes GCD.
- While traversing the array:
- Keep calculating GCD till the g becomes 1.
- If g equals 1, then increase the cnt variable, and set g to 0.
- In the end, return the number of subarrays.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int count_subarrays( int arr[], int n)
{
int g = 0;
int cnt = 0;
for ( int i = 0; i < n; ++i) {
g = gcd(g, arr[i]);
if (g == 1) {
cnt++;
g = 0;
}
}
return cnt;
}
int main()
{
int arr[] = { 9, 36, 8, 3, 7, 2, 13, 26, 5 };
int n = ( sizeof (arr) / sizeof (arr[0]));
int ans = count_subarrays(arr, n);
cout << ans;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static int count_subarrays( int arr[], int n)
{
int g = 0 ;
int cnt = 0 ;
for ( int i = 0 ; i < n; ++i) {
g = gcd(g, arr[i]);
if (g == 1 ) {
cnt++;
g = 0 ;
}
}
return cnt;
}
public static void main(String[] args)
{
int arr[] = { 9 , 36 , 8 , 3 , 7 , 2 , 13 , 26 , 5 };
int n = arr.length;
int ans = count_subarrays(arr, n);
System.out.print(ans);
}
}
|
Python3
class GFG :
@staticmethod
def gcd( a, b) :
if (b = = 0 ) :
return a
return GFG.gcd(b, a % b)
@staticmethod
def count_subarrays( arr, n) :
g = 0
cnt = 0
i = 0
while (i < n) :
g = GFG.gcd(g, arr[i])
if (g = = 1 ) :
cnt + = 1
g = 0
i + = 1
return cnt
@staticmethod
def main( args) :
arr = [ 9 , 36 , 8 , 3 , 7 , 2 , 13 , 26 , 5 ]
n = len (arr)
ans = GFG.count_subarrays(arr, n)
print (ans, end = "")
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
public class GFG
{
public static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int count_subarrays( int []arr, int n)
{
int g = 0;
int cnt = 0;
for ( int i = 0; i < n; ++i) {
g = gcd(g, arr[i]);
if (g == 1) {
cnt++;
g = 0;
}
}
return cnt;
}
public static void Main( string [] args)
{
int []arr = { 9, 36, 8, 3, 7, 2, 13, 26, 5 };
int n = arr.Length;
int ans = count_subarrays(arr, n);
Console.WriteLine(ans);
}
}
|
Javascript
function gcd( a, b)
{
if (b == 0)
return a;
return gcd(b,a % b);
}
function count_subarrays(arr, n)
{
let g = 0;
let cnt = 0;
for (let i = 0; i < n; ++i) {
g = gcd(g, arr[i]);
if (g == 1) {
cnt++;
g = 0;
}
}
return cnt;
}
let arr = [ 9, 36, 8, 3, 7, 2, 13, 26, 5 ];
let n = arr.length;
let ans = count_subarrays(arr, n);
console.log(ans);
|
Time Complexity: O(N*log(M)), where N is the size of the array and M is the minimum element in the given array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...