Home > Backend Development > C++ > body text

The longest subarray whose greatest common divisor is greater than 1

王林
Release: 2023-09-18 22:17:04
forward
1166 people have browsed it

The longest subarray whose greatest common divisor is greater than 1

An array is a collection of similar data stored in adjacent memory locations in a contiguous manner. By defining the offset value as a specific base value for the database, it is easier to evaluate the specific position of each element. The base value for that particular index is zero, and the offset value is the difference between the two particular indices. A subarray is a part of a specific array and can be defined as a set of variables, labeled with multiple values. The longest subarray refers to an array in which all elements in the array are greater than K. The sum of the maximum sum subarray here is -

  • Less than

  • in a given dataset
  • Equal to the given data set.

  • Less than

  • in a given dataset

To find the length of the longest subarray, we just need to find the total number of 1's in a specific subarray. NOTE: The count should be greater than the count of zero. The greatest common divisor is a mathematical phenomenon in which we find the largest integer value that can divide each integer in the input with a zero remainder. The condition here is that "the greatest common divisor is greater than 1". This means that this particular number here only has at least one common divisor between the given inputs.

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.
Copy after login

Today in this article, we will learn how to find the longest subarray whose greatest common divisor is greater than 1 using the C programming environment.

Algorithm to find the longest subarray whose GCD is greater than 1

In this particular algorithm, we can find the greatest common value of the longest subarray containing greater than 1.

  • Step one - start.

  • Step 2 - Declare process variables.

  • Step 3 - Set and initialize it to zero value.

  • Step 4 - Create a function to evaluate the maximum length of this subarray.

  • Step 5 - Include a vector as argument.

  • Step 6 - Create a variable to get the answer.

  • Step 7 - Set and initialize it to zero value.

  • Step 8 - Store the value of the longest subarray with GCD > 1 value.

  • Step 9 - Iterate the loop to find the greatest common divisor of each subarray.

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

  • Step 11 - If the greatest common divisor of the subarrays is greater than 1, save the answer.

  • Step 12 - Return the answer.

  • Step 13 - Otherwise, run the loop again and iterate.

  • Step 14 - Terminate after the process is complete.

Syntax for finding the longest subarray whose GCD is 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;
Copy after login

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

method:

  • Method 1 − A C program to find the longest subarray whose greatest common divisor is greater than 1 through the naive method.

  • Method 2 - C program to find the greatest common divisor of an array greater than 1.

C program to find subarrays with longest common divisor greater than 1 using naive method

In this C code, we take the naive approach of finding the GCD value with the longest subarray greater than 1 by generating all possible subarrays of the given array.

The Chinese translation of

Example 1

is:

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;
}
Copy after login

Output

3
Copy after login

C program to find the greatest common divisor of an array greater than 1

In this C code, we try to calculate the greatest common divisor and it has the ability to check if it is greater than 1.

Example 2

is translated as:

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);
}
Copy after login

Output

5
2
1
Copy after login

in conclusion

Through this discussion, we can find out how to find the longest subarray whose GCD is greater than 1. Hopefully, the algorithm and C code written will clearly show you how this process works in the real world.

The above is the detailed content of The longest subarray whose greatest common divisor is greater than 1. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!