📜  所有大小为 K 的子数组的 GCD

📅  最后修改于: 2021-09-06 11:06:01             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是打印大小为K的所有子数组的 GCD。

例子:

方法:想法是生成所有大小为K的子数组并打印每个子数组的GCD。为了有效地计算每个子阵列的 GCD,我们的想法是使用 GCD 的以下属性。

请按照以下步骤解决问题:

  1. 初始化一个变量,比如gcd ,以存储当前子数组的GCD
  2. 从给定数组生成K 个长度的子数组。
  3. 应用GCD的上述性质,计算每个子阵列的GCD,并打印得到的结果。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print the gcd
// of each subarray of length K
void printSub(int arr[], int N,
              int K)
{
    for (int i = 0; i <= N - K; i++) {
 
        // Store GCD of subarray
        int gcd = arr[i];
 
        for (int j = i + 1; j < i + K;
             j++) {
 
            // Update GCD of subarray
            gcd = __gcd(gcd, arr[j]);
        }
 
        // Print GCD of subarray
        cout << gcd << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 4, 3, 9, 14,
                  20, 25, 17 };
    int K = 2;
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    printSub(arr, N, K);
}


Java
// Java program to implement
// the above approach
class GFG{
 
static int __gcd(int a, int b)
{
  if (b == 0)
    return a;
  return __gcd(b, a % b);
}
   
// Function to print the gcd
// of each subarray of length K
static void printSub(int arr[],
                     int N, int K)
{
  for (int i = 0; i <= N - K; i++)
  {
    // Store GCD of subarray
    int gcd = arr[i];
 
    for (int j = i + 1; j < i + K; j++)
    {
      // Update GCD of subarray
      gcd = __gcd(gcd, arr[j]);
    }
 
    // Print GCD of subarray
    System.out.print(gcd + " ");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  int arr[] = {2, 4, 3, 9,
               14, 20, 25, 17};
  int K = 2;
  int N = arr.length;
  printSub(arr, N, K);
}
}
 
// This code is contributed by Chitranayal


Python3
# Python3 program to implement
# the above approach
from math import gcd
 
# Function to print the gcd
# of each subarray of length K
def printSub(arr, N, K):
     
    for i in range(N - K + 1):
 
        # Store GCD of subarray
        g = arr[i]
 
        for j in range(i + 1, i + K):
             
            # Update GCD of subarray
            g = gcd(g, arr[j])
 
        # Print GCD of subarray
        print(g, end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 2, 4, 3, 9, 14,
            20, 25, 17 ]
    K = 2
    N = len(arr)
 
    printSub(arr, N, K)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
static int __gcd(int a, int b)
{
  if (b == 0)
    return a;
  return __gcd(b, a % b);
}
   
// Function to print the gcd
// of each subarray of length K
static void printSub(int []arr,
                     int N, int K)
{
  for (int i = 0; i <= N - K; i++)
  {
    // Store GCD of subarray
    int gcd = arr[i];
 
    for (int j = i + 1; j < i + K; j++)
    {
      // Update GCD of subarray
      gcd = __gcd(gcd, arr[j]);
    }
 
    // Print GCD of subarray
    Console.Write(gcd + " ");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int []arr = {2, 4, 3, 9,
               14, 20, 25, 17};
  int K = 2;
  int N = arr.Length;
  printSub(arr, N, K);
}
}
 
 
// This code is contributed by Princi Singh


Javascript


输出:
2 1 3 1 2 5 1

时间复杂度: O((N – K + 1) * K)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live