📜  GCD 等于给定数组的 GCD 的最小子序列

📅  最后修改于: 2021-09-05 08:52:41             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到给定数组的最小子序列,该子序列的 GCD 等于给定数组的 GCD 如果存在多个这样的子序列,则打印它们中的任何一个。

例子:

朴素的方法:解决这个问题的最简单的方法是生成给定数组的所有可能的子序列,并计算每个子序列的 GCD。打印给定数组的 gcd 等于 gcd 的最小子序列。

时间复杂度: O(2 N * N * log X),其中 X 是给定数组的最大元素。
辅助空间: O(1)

高效的方法:为了优化上述方法,该想法基于以下观察:

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

  • 初始化一个变量gcdArr来存储数组的 GCD。
  • 遍历给定的数组并检查是否有任何数组元素等于gcdArr 。如果发现为真,则打印该元素。
  • 否则,从给定的数组中打印一对,其 gcd 等于gcdArr

下面是上述方法的实现。

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print
// the smallest subsequence
// that satisfies the condition
void printSmallSub(int arr[], int N)
{
    // Stores gcd of the array.
    int gcdArr = 0;
 
    // Traverse the given array
    for (int i = 0; i < N; i++) {
 
        // Update gcdArr
        gcdArr = __gcd(gcdArr, arr[i]);
    }
 
    // Traverse the given array.
    for (int i = 0; i < N; i++) {
 
        // If current element
        // equal to gcd of array.
        if (arr[i] == gcdArr) {
            cout << arr[i] << " ";
            return;
        }
    }
 
    // Generate all possible pairs.
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N;
             j++) {
 
            // If gcd of current pair
            // equal to gcdArr
            if (__gcd(arr[i], arr[j])
                == gcdArr) {
 
                // Print current pair
                // of the array
                cout << arr[i] << " " << arr[j];
                return;
            }
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 6, 12 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    printSmallSub(arr, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to calculate gcd
// of two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
         
    return gcd(b, a % b);
}
 
// Function to print
// the smallest subsequence
// that satisfies the condition
static void printSmallSub(int[] arr, int N)
{
     
    // Stores gcd of the array.
    int gcdArr = 0;
 
    // Traverse the given array
    for(int i = 0; i < N; i++)
    {
         
        // Update gcdArr
        gcdArr = gcd(gcdArr, arr[i]);
    }
 
    // Traverse the given array.
    for(int i = 0; i < N; i++)
    {
         
        // If current element
        // equal to gcd of array.
        if (arr[i] == gcdArr)
        {
            System.out.print(arr[i] + " ");
            return;
        }
    }
 
    // Generate all possible pairs.
    for(int i = 0; i < N; i++)
    {
        for(int j = i + 1; j < N; j++)
        {
             
            // If gcd of current pair
            // equal to gcdArr
            if (gcd(arr[i], arr[j]) == gcdArr)
            {
                 
                // Print current pair
                // of the array
                System.out.print(arr[i] + " " +
                                 arr[j]);
                return;
            }
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 4, 6, 12 };
    int N = arr.length;
 
    printSmallSub(arr, N);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program to implement
# the above approach
import math
 
# Function to print the
# smallest subsequence
# that satisfies the condition
def printSmallSub(arr, N):
     
    # Stores gcd of the array.
    gcdArr = 0
 
    # Traverse the given array
    for i in range(0, N):
         
        # Update gcdArr
        gcdArr = math.gcd(gcdArr, arr[i])
 
    # Traverse the given array.
    for i in range(0, N):
         
        # If current element
        # equal to gcd of array.
        if (arr[i] == gcdArr):
            print(arr[i], end = " ")
            return
 
        # Generate all possible pairs.
        for i in range(0, N):
            for j in range(i + 1, N):
                 
                # If gcd of current pair
                # equal to gcdArr
                if (math.gcd(arr[i],
                             arr[j]) == gcdArr):
 
                    # Print current pair
                    # of the array
                    print(arr[i], end = " ")
                    print(arr[j], end = " ")
                    return
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 4, 6, 12 ]
    N = len(arr)
     
    printSmallSub(arr, N)
 
# This code is contributed by akhilsaini


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to calculate gcd
// of two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
         
    return gcd(b, a % b);
}
 
// Function to print the
// smallest subsequence
// that satisfies the condition
static void printSmallSub(int[] arr, int N)
{
     
    // Stores gcd of the array.
    int gcdArr = 0;
 
    // Traverse the given array
    for(int i = 0; i < N; i++)
    {
         
        // Update gcdArr
        gcdArr = gcd(gcdArr, arr[i]);
    }
 
    // Traverse the given array.
    for(int i = 0; i < N; i++)
    {
         
        // If current element
        // equal to gcd of array.
        if (arr[i] == gcdArr)
        {
            Console.Write(arr[i] + " ");
            return;
        }
    }
 
    // Generate all possible pairs.
    for(int i = 0; i < N; i++)
    {
        for(int j = i + 1; j < N; j++)
        {
             
            // If gcd of current pair
            // equal to gcdArr
            if (gcd(arr[i], arr[j]) == gcdArr)
            {
                 
                // Print current pair
                // of the array
                Console.Write(arr[i] + " " +
                              arr[j]);
                return;
            }
        }
    }
}
 
// Driver Code
public static void Main()
{
 
    int[] arr = { 4, 6, 12 };
    int N = arr.Length;
 
    printSmallSub(arr, N);
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
4 6

时间复杂度: (N 2 * log X),其中 X 是给定数组的最大元素。
辅助空间: O(1)

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