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++
#include <iostream>
using namespace std;
string canSplitArray( int arr[], int n,
int k)
{
for ( int i = 0; i < n; i++) {
if (arr[i] <= k) {
return "No" ;
}
}
return "Yes" ;
}
int main()
{
int arr[] = { 2, 4, 6, 1, 8, 16 };
int N = sizeof arr / sizeof arr[0];
int K = 3;
cout << canSplitArray(arr, N, K);
}
|
Java
import java.io.*;
class GFG{
static String canSplitArray( int arr[],
int n, int k)
{
for ( int i = 0 ; i < n; i++)
{
if (arr[i] <= k)
{
return "No" ;
}
}
return "Yes" ;
}
public static void main (String[] args)
{
int arr[] = { 2 , 4 , 6 , 1 , 8 , 16 };
int N = arr.length;
int K = 3 ;
System.out.println(canSplitArray(arr, N, K));
}
}
|
Python3
def canSplitArray(arr, n, k):
for i in range (n):
if (arr[i] < = k):
return "No"
return "Yes"
if __name__ = = '__main__' :
arr = [ 2 , 4 , 6 , 1 , 8 , 16 ]
N = len (arr)
K = 3
print (canSplitArray(arr, N, K))
|
C#
using System;
class GFG{
static String canSplitArray( int []arr,
int n, int k)
{
for ( int i = 0; i < n; i++)
{
if (arr[i] <= k)
{
return "No" ;
}
}
return "Yes" ;
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 6, 1, 8, 16 };
int N = arr.Length;
int K = 3;
Console.WriteLine(canSplitArray(arr, N, K));
}
}
|
Javascript
<script>
function canSplitArray(arr, n, k)
{
for (let i = 0; i < n; i++)
{
if (arr[i] <= k)
{
return "No" ;
}
}
return "Yes" ;
}
let arr = [ 2, 4, 6, 1, 8, 16 ];
let N = arr.length;
let K = 3;
document.write(canSplitArray(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...