给定N个整数的数组arr []和一个正整数K ,任务是检查是否有可能将此数组拆分为不同的连续子数组,以使每个子数组的所有元素的最大公约数大于K。
注意:每个数组元素都可以是一个子数组的一部分。
例子:
Input: arr[] = {3, 2, 4, 4, 8}, K = 1
Output: Yes
Explanation:
One valid split is [3], [2, 4], [4, 8] with GCD 3, 2 and 4 respectively.
Another Valid Split is [3], [2, 4], [4], [8] with GCD 3, 2, 4 and 8 respectively.
Therefore, the given array can be split into subarrays having GCD > K.
Input: arr[] = {2, 4, 6, 1, 8, 16}, K = 3
Output: No
方法:可以使用以下观察结果解决此问题:
- 如果发现任何数组元素小于或等于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)