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

📅  最后修改于: 2021-10-26 06:53:22             🧑  作者: 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程