Given an array **arr[]** of **N** integers and a positive integer **K**, the task is to check if it is possible to split this array into distinct contiguous subarrays such that the Greatest Common Divisor of all elements of each subarray is greater than **K**.

**Note: **Each array element can be a part of exactly one subarray.

**Examples:**

Input:arr[] = {3, 2, 4, 4, 8}, K = 1Output:YesExplanation:

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 = 3Output:No

**Approach:** This problem can be solved using the following observations:

- If any array element is found to be
**less than or equal to K**, then the answer is always “No”. This is because the subarray that contains this number will always have GCD less than or equal to**K**. - If no array element is found to be less than or equal to
**K**, then it is always possible to divide the entire array into**N**subarrays each of size at least**1**whose GCD is always greater than**K**.

Therefore, from the above observations, the idea is to traverse the given array and check that if there exists any element in the array which is less than or equal to **K**. If found to be true, then print **“No”**. Otherwise print **“Yes”**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <iostream> ` `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); ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

No

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Check if an array can be split into 3 subsequences of equal sum or not
- Check if an array can be split into subsets of K consecutive elements
- Split an array into two equal Sum subarrays
- Split array into K disjoint subarrays such that sum of each subarray is odd.
- Split array into two subarrays such that difference of their maximum is minimum
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Split array into two subarrays such that difference of their sum is minimum
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Check if two items can be selected from two different categories without exceeding price
- Check if a string can be split into two strings with same number of K-frequent characters
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.