Sunday, November 16, 2025
HomeData Modelling & AICheck if an array can be split into subarrays with GCD exceeding...

Check if an array can be split into subarrays with GCD exceeding K

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 = 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

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);
}


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




<script>
 
// JavaScript implementation of the above approach
 
// Function to check if it is possible
// to split an array into subarrays
// having GCD at least K
function canSplitArray(arr, n, k)
{
       
    // Iterate over the array arr[]
    for(let 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
         
    // Given array arr[]
    let arr = [ 2, 4, 6, 1, 8, 16 ];
       
    // Length of the array
    let N = arr.length;
       
    // Given K
    let K = 3;
       
    // Function call
    document.write(canSplitArray(arr, N, K));
       
    // This code is contributed by code_hunt.
</script>


Output: 

No

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32402 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6769 POSTS0 COMMENTS
Nicole Veronica
11920 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11990 POSTS0 COMMENTS
Shaida Kate Naidoo
6897 POSTS0 COMMENTS
Ted Musemwa
7150 POSTS0 COMMENTS
Thapelo Manthata
6851 POSTS0 COMMENTS
Umr Jansen
6843 POSTS0 COMMENTS