📜  带有任意子集的GCD的数组属于给定数组

📅  最后修改于: 2021-06-26 19:00:48             🧑  作者: Mango

给定一组N个元素,使得N \in [1, 1000]任务是生成一个数组,以使生成的数组的任何子集的GCD都位于给定的元素集中。生成的数组的长度不得超过GCD集合长度的三倍

先决条件:阵列的GCD |数组的子集

例子 :

Input : 3
        1 2 7
Output :  1 1 2 1 7

Input : 4
        2 4 6 12
Output : 2 2 4 2 6 2 12


Input : 5
        2 5 6 7 11
Output : No array can be build

解释 :
计算数组或本例中的集合的GCD。现在,首先对给定的GCD集进行排序。如果此集合的GCD等于给定集合的最小数目,则只需将该GCD放在每个数字之间即可。但是,如果该GCD不是给定集合的最小元素,那么不幸的是“无法构建数组”。

C++
// C++ implementation to generate the 
// required array
#include 
using namespace std;
  
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
       return b;       
    return gcd(b % a, a);
}
  
// Function to find gcd of
// array of numbers
int findGCD(vector arr, int n)
{
    int result = arr[0];    
    for (int i = 1; i < n; i++)
        result = gcd(arr[i], result);
    return result;
}
  
// Function to generate the array
// with required constraints.
void compute(vector arr, int n)
{
    vector answer;
      
    // computing GCD of the given set
    int GCD_of_array = findGCD(arr, n);
  
    // Solution exists if GCD of array is equal
    // to the minimum element of the array
    if(GCD_of_array == arr[0])
    {
        answer.push_back(arr[0]);
        for(int i = 1; i < n; i++)
        {
            answer.push_back(arr[0]);
            answer.push_back(arr[i]);
        }
      
        // Printing the built array
        for (int i = 0; i < answer.size(); i++)
            cout << answer[i] << " ";
    }
    else
        cout << "No array can be build";
}
  
// Driver function
int main()
{
  
    // Taking in the input and initializing
    // the set STL set in cpp has a property
    // that it maintains the elements in
    // sorted order, thus we do not need 
    // to sort them externally
    int n = 3;
    int input[]= {2, 5, 6, 7, 11};
    set GCD(input, input + n);
    vector arr;
    set::iterator it;
      
    for(it = GCD.begin(); it!= GCD.end(); ++it)
        arr.push_back(*it);
  
    // Calling the computing function.
    compute(arr,n);
      
    return 0;
}


Java
// Java implementation 
// to generate the 
// required array
import java.io.*;
import java.util.*;
  
class GFG
{
// Function to return
// gcd of a and b
static int gcd(int a,
               int b)
{
    if (a == 0)
    return b;     
    return gcd(b % a, a);
}
  
// Function to find gcd 
// of array of numbers
public static int findGCD(ArrayList 
                                 arr, int n)
{
    int result = arr.get(0); 
    for (int i = 1; i < n; i++)
        result = gcd(arr.get(i), 
                    result);
    return result;
}
  
// Function to generate 
// the array with required 
// constraints.
public static void compute(ArrayList
                                  arr, int n)
{
    ArrayList answer = 
                    new ArrayList();
      
    // computing GCD of
    // the given set
    int GCD_of_array = findGCD(arr, n);
  
    // Solution exists if GCD
    // of array is equal to the 
    // minimum element of the array
    if(GCD_of_array == arr.get(0))
    {
        answer.add(arr.get(0));
        for(int i = 1; i < n; i++)
        {
            answer.add(arr.get(0));
            answer.add(arr.get(i));
        }
      
        // Printing the 
        // built array
        for (int i = 0; 
                 i < answer.size(); i++)
            System.out.print(answer.get(i) + " ");
    }
    else
        System.out.print("No array " +
                      "can be build");
}
  
// Driver Code
public static void main(String args[])
{
  
    // Taking in the input and 
    // initializing the set STL
    // set in cpp has a property
    // that it maintains the 
    // elements in sorted order, 
    // thus we do not need to 
    // sort them externally
    int n = 3;
    Integer input[]= {2, 5, 6, 7, 11};
    HashSet GCD = new HashSet
                        (Arrays.asList(input));
    ArrayList arr = 
                new ArrayList();
      
    for (int v : GCD)
        arr.add(v);
  
    // Calling the
    // computing function.
    compute(arr, n);
}
}
  
// This code is contributed by
// Manish Shaw(manishshaw1)


Python3
from math import gcd
# Python 3 implementation to generate the 
# required array
  
# Function to find gcd of
# array of numbers
def findGCD(arr, n):
    result = arr[0] 
    for i in range(1,n):
        result = gcd(arr[i], result)
    return result
  
# Function to generate the array
# with required constraints.
def compute(arr, n):
    answer = []
      
    # computing GCD of the given set
    GCD_of_array = findGCD(arr, n)
  
    # Solution exists if GCD of array is equal
    # to the minimum element of the array
    if(GCD_of_array == arr[0]):
        answer.append(arr[0])
        for i in range(1,n):
            answer.append(arr[0])
            answer.append(arr[i])
      
        # Printing the built array
        for i in range(len(answer)):
            print(answer[i],end = " ")
  
      
    else:
        print("No array can be build")
  
# Driver function
if __name__ == '__main__':
    # Taking in the input and initializing
    # the set STL set in cpp has a property
    # that it maintains the elements in
    # sorted order, thus we do not need 
    # to sort them externally
    n = 3
    input = [2, 5, 6, 7, 11]
    GCD = set()
    for i in range(len(input)):
        GCD.add(input[i])
  
    arr = []
  
    for i in GCD:
        arr.append(i)
  
    # Calling the computing function.
    compute(arr,n)
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation 
// to generate the 
// required array
using System;
using System.Collections.Generic;
  
class GFG
{
    // Function to return
    // gcd of a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
        return b;     
        return gcd(b % a, a);
    }
      
    // Function to find gcd 
    // of array of numbers
    static int findGCD(List arr, 
                               int n)
    {
        int result = arr[0]; 
        for (int i = 1; i < n; i++)
            result = gcd(arr[i], 
                         result);
        return result;
    }
      
    // Function to generate 
    // the array with required 
    // constraints.
    static void compute(List arr, 
                                int n)
    {
        List answer = new List();
          
        // computing GCD of
        // the given set
        int GCD_of_array = findGCD(arr, n);
      
        // Solution exists if GCD
        // of array is equal to the 
        // minimum element of the array
        if(GCD_of_array == arr[0])
        {
            answer.Add(arr[0]);
            for(int i = 1; i < n; i++)
            {
                answer.Add(arr[0]);
                answer.Add(arr[i]);
            }
          
            // Printing the 
            // built array
            for (int i = 0; i < answer.Count; i++)
                Console.Write(answer[i] + " ");
        }
        else
            Console.Write("No array " +
                          "can be build");
    }
      
    // Driver Code
    static void Main()
    {
      
        // Taking in the input and 
        // initializing the set STL
        // set in cpp has a property
        // that it maintains the 
        // elements in sorted order, 
        // thus we do not need to 
        // sort them externally
        int n = 3;
        int []input= new int[]{2, 5, 6, 7, 11};
        HashSet GCD = new HashSet(input);
        List arr = new List();
          
        foreach (int b in GCD)
            arr.Add(b);
      
        // Calling the
        // computing function.
        compute(arr, n);
    }
}
  
// This code is contributed by
// Manish Shaw(manishshaw1)


输出:
No array can be build

时间复杂度: O(nlog(n)),其中n是给定数组的大小。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。