📌  相关文章
📜  检查是否可以将数组拆分为GCD超过K的子数组

📅  最后修改于: 2021-05-19 17:54:19             🧑  作者: Mango

给定N个整数的数组arr []和一个正整数K ,任务是检查是否有可能将此数组拆分为不同的连续子数组,以使每个子数组的所有元素的最大公约数大于K。

注意:每个数组元素都可以是一个子数组的一部分。

例子:

方法:可以使用以下观察结果解决此问题:

  • 如果发现任何数组元素小于或等于K ,则答案始终为“否”。这是因为包含此数字的子数组将始终具有小于或等于K的GCD。
  • 如果没有找到小于或等于K的数组元素,则始终可以将整个数组划分为N个子数组,每个子数组的大小至少为1,其GCD总是大于K。

因此,根据上述观察,我们的想法是遍历给定的数组,并检查数组中是否存在小于或等于K的元素。如果发现是真的,则打印“否” 。否则,打印“是”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if it is possible
// to split an array into subarrays
// having GCD at least K
string canSplitArray(int arr[], int n,
                     int k)
{
 
    // Iterate over the array arr[]
    for (int i = 0; i < n; i++) {
 
        // If the current element
        // is less than or equal to k
        if (arr[i] <= k) {
            return "No";
        }
    }
 
    // If no array element is found
    // to be less than or equal to k
    return "Yes";
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 2, 4, 6, 1, 8, 16 };
 
    int N = sizeof arr / sizeof arr[0];
 
    // Given K
    int K = 3;
 
    // Function Call
    cout << canSplitArray(arr, N, K);
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to check if it is possible
// to split an array into subarrays
// having GCD at least K
static String canSplitArray(int arr[],
                            int n, int k)
{
     
    // Iterate over the array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // If the current element
        // is less than or equal to k
        if (arr[i] <= k)
        {
            return "No";
        }
    }
     
    // If no array element is found
    // to be less than or equal to k
    return "Yes";
}
 
// Driver code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 2, 4, 6, 1, 8, 16 };
     
    // Length of the array
    int N = arr.length;
     
    // Given K
    int K = 3;
     
    // Function call
    System.out.println(canSplitArray(arr, N, K));
}
}
 
// This code is contributed by jana_sayantan


Python3
# Python3 program for the above approach
 
# Function to check if it is possible
# to split an array into subarrays
# having GCD at least K
def canSplitArray(arr, n, k):
 
    # Iterate over the array arr[]
    for i in range(n):
 
        # If the current element
        # is less than or equal to k
        if (arr[i] <= k):
            return "No"
 
    # If no array element is found
    # to be less than or equal to k
    return "Yes"
 
# Driver Code
if __name__ == '__main__':
 
    # Given array arr[]
    arr = [ 2, 4, 6, 1, 8, 16 ]
 
    N = len(arr)
 
    # Given K
    K = 3
 
    # Function call
    print(canSplitArray(arr, N, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if it is possible
// to split an array into subarrays
// having GCD at least K
static String canSplitArray(int []arr,
                            int n, int k)
{
     
    // Iterate over the array []arr
    for(int i = 0; i < n; i++)
    {
         
        // If the current element
        // is less than or equal to k
        if (arr[i] <= k)
        {
            return "No";
        }
    }
     
    // If no array element is found
    // to be less than or equal to k
    return "Yes";
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 2, 4, 6, 1, 8, 16 };
     
    // Length of the array
    int N = arr.Length;
     
    // Given K
    int K = 3;
     
    // Function call
    Console.WriteLine(canSplitArray(arr, N, K));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
No

时间复杂度: O(N)
辅助空间: O(1)