给定数组arr [] ,任务是查找GCD值等于1的子数组的数量。
例子:
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
方法:主要观察结果是,如果知道子数组arr [l…r]的所有元素的GCD,那么可以获得子数组arr [l…r + 1]的所有元素的GCD通过简单地使用arr [r + 1]来获取先前子数组的GCD即可。
因此,对于每个索引i ,保持向前迭代并计算从索引i到j的GCD并检查其是否等于1 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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;
}
Java
// 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
Python3
# 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
C#
// 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
输出:
6
时间复杂度: O(N 2 )