Longest Subarray with GCD Greater than 1

An array is a collection of similar data sets which stored at adjacent memory locations in a continuous manner. It makes the process easier to evaluate the particular position of each and every element by defining an offset value to the particular base value of a database. The base value of that particular index is zero and an offset is a value of the difference of two particular indexes. A subarray is a section of a particular array which can be defined as a set of variables collectively with a label of multiple values. The longest subarray means in which array the all elements are greater than K. Here the maximum sum subarray having the sum is –

• Less than of the given data set.

• Equal to the given data set.

• Less than of the given data set.

To find the length of a longest subarray, we just need to find out the total count of 1's in a particular subarray. Note: The count should be more than the count of zero. Greatest Common Divisor is a mathematical phenomenon where we find the largest integer value that can divide each of the inputted integers with a zero remainder. The condition here is, the "GCD is greater than 1". It means, here the particular numbers have only at least one common divisor present between the given input.

Input (array) : arr[] = {4, 3, 2, 2}
Output (after the process with sub-array operation) : 2
If we consider the subarray as {2, 2}, then we will get 2 as GCD. Which is > 1, is of maximum length.


Today in this article we will learn how to find a longest subarray with GCD greater than 1 by using c++ coding environment.

Algorithm to find the longest subarray with GCD greater than 1

In this particular algorithm, we can find out the longest subarray which contains the GCD value of greater than 1.

• Step 1 − Start.

• Step 2 − Declare the variable for the process.

• Step 3 − Set and initialize it to the zero value.

• Step 4 − Create a function to evaluate the maximum length of that subarray.

• Step 5 − Include a vector with it as an argument.

• Step 6 − Create a variable for getting answer.

• Step 7 − Set and initialize it to the zero value.

• Step 8 − Store the value of the longest subarray having a value of GCD > 1.

• Step 9 − Iterate the loop to find the GCD for each subarray.

• Step 10 − Replace the answer with the length value of the subarray.

• Step 11 − If, the GCD of the subarray is greater than one then save the answer.

• Step 12 − Return the answer.

• Step 13 − Else, run the loop again and iterate it.

• Step 14 − Terminate after the process done.

Syntax to find the longest subarray with GCD greater than 1

int n;
cin >> n;

const int MAX_NUM = 100 * 1000;
static int dp[MAX_NUM];

for(int i = 0; i < n; ++i){
int x;
cin >> x;

int cur = 1;
vector<int> d;
for(int i = 2; i * i <= x; ++i){
if(x % i == 0){
cur = max(cur, dp[i] + 1);
cur = max(cur, dp[x / i] + 1);
d.push_back(i);
d.push_back(x / i);
}
}
if(x > 1){
cur = max(cur, dp[x] + 1);
d.push_back(x);
}

for(int j : d){
dp[j] = cur;
}
}
cout << *max_element(dp, dp + MAX_NUM) << endl;


By following the above mentioned algorithm, we have written the possible syntax here to find the longest subarray with the value of GCD, greater than 1.

Approach:-

• Approach 1 − C++ program to find the longest subarray with GCD greater than 1 by naive approach.

• Approach 2 − C++ program to find GCD of array is greater than one.

C++ program to find the longest subarray with GCD greater than 1 by naive approach

Here in this C++ code, we have applied the naive approach, to find the longest subarray present with the GCD greater than 1 value by generating all possible subarrays of the given array.

Example 1

#include <bits/stdc++.h>
using namespace std;
void maxSubarrayLen(int arr[], int n) {
int maxLen = 0;
for (int i = 0; i < n; i++) {
int gcd = 0;
for (int j = i; j < n; j++) {
gcd = __gcd(gcd, arr[j]);
if (gcd > 1)
maxLen = max(maxLen, j - i + 1);
else
break;
}
}
cout << maxLen;
}
int main() {
int arr[] = { 410, 16, 7, 180, 222, 10, 33 };
int N = sizeof(arr) / sizeof(int);
maxSubarrayLen(arr, N);

return 0;
}


Output

3


C++ program to find GCD of array is greater than one

In this C++ code, we have tried to calculate the GCD and it has that ability to check whether it is greater than 1 or not.

Example 2

#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b){
if (a == 0)
return b;
return gcd(b%a, a);
}
void bestArray(int arr[], int n){
bool even[n] = {false};
int ans = 0;
for(int i = 0; i < n; i++){
ans = gcd(ans, arr[i]);
if(arr[i] % 2 == 0)
even[i] = true;
}
if(ans > 1)
cout << 0 << endl;
else {
ans = 0;
for(int i = 0; i < n-1; i++){
if(!even[i]){
even[i] = true;
even[i+1] = true;
if(arr[i+1]%2 != 0){
ans+=1;
}
else
ans+=2;
}
}
if(!even[n-1]){
ans+=2;
}
cout << ans << endl;
}
}
int main(){
int arr[] = {16, 10, 07, 81, 88, 32, 3, 42, 25};
int n = 9;
bestArray(arr, n);

int arr1[] = {16, 7};
n = 2;
bestArray(arr1, n);

int arr2[] = {10, 97, 2001};
n = 3;
bestArray(arr2, n);
}


Output

5
2
1


Conclusion

From this discussion, we can find out how to find the longest subarray with GCD greater than 1 value. Hope the written algorithm and C++ code will give a clear view to make you understand how this process works in the real world.

Updated on: 06-Apr-2023

168 Views