Check if an array can be split into subsets of K consecutive elements
Last Updated :
17 Apr, 2024
Given an array arr[] and integer K, the task is to split the array into subsets of size K, such that each subset consists of K consecutive elements.
Examples:
Input: arr[] = {1, 2, 3, 6, 2, 3, 4, 7, 8}, K = 3
Output: true
Explanation:
The given array of length 9 can be split into 3 subsets {1, 2, 3}, {2, 3, 4} and {6, 7, 8} such that each subset consists of 3 consecutive elements.
Input: arr[] = [1, 2, 3, 4, 5], K = 4
Output: false
Explanation:
The given array of length 5 cannot be split into subsets of 4.
Approach
Follow the steps to solve the problem:
Instead of a HashMap, you can use an array or another data structure to store the frequencies of array elements. This can lead to better performance since direct array access is typically faster than hashmap lookups.
Instead of traversing the entire HashMap, you can optimize the traversal by iterating over the array directly and checking for consecutive elements.
The description mentions grouping elements with their next (K - 1) consecutive elements. It's important to clarify whether these consecutive elements are strictly consecutive or if there can be gaps between them.
- Sort the array to simplify consecutive element checking.
- Iterate through the sorted array and check if each element can be grouped with the next (K - 1) elements.
- If any element cannot be grouped into a subset of K consecutive elements, return False. Otherwise, return True.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool canSplitArray(vector<int>& arr, int K) {
int n = arr.size();
// Sort the array to simplify consecutive element checking
sort(arr.begin(), arr.end());
// Iterate through the sorted array and check for consecutive elements
for (int i = 0; i < n - K + 1; i++) {
// Check if each element can be grouped with the next (K - 1) elements
if (arr[i + K - 1] - arr[i] != K - 1) {
return false;
}
}
return true;
}
int main() {
vector<int> arr1 = {1, 2, 3, 6, 2, 3, 4, 7, 8};
int K1 = 3;
cout << "Output for arr1: " << boolalpha << canSplitArray(arr1, K1) << endl; // Output: true
vector<int> arr2 = {1, 2, 3, 4, 5};
int K2 = 4;
cout << "Output for arr2: " << boolalpha << canSplitArray(arr2, K2) << endl; // Output: false
return 0;
}
Java
import java.util.Arrays;
public class SplitArrayIntoSubsets {
public static boolean canSplitArray(int[] arr, int K) {
int n = arr.length;
// Sort the array to simplify consecutive element checking
Arrays.sort(arr);
// Iterate through the sorted array and check for consecutive elements
for (int i = 0; i < n - K + 1; i++) {
// Check if each element can be grouped with the next (K - 1) elements
if (arr[i + K - 1] - arr[i] != K - 1) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 6, 2, 3, 4, 7, 8};
int K1 = 3;
System.out.println("Output for arr1: " + canSplitArray(arr1, K1)); // Output: true
int[] arr2 = {1, 2, 3, 4, 5};
int K2 = 4;
System.out.println("Output for arr2: " + canSplitArray(arr2, K2)); // Output: false
}
}
Python3
def can_split_array(arr, K):
arr.sort()
for i in range(len(arr) - K + 1):
if arr[i + K - 1] - arr[i] != K - 1:
return False
return True
arr1 = [1, 2, 3, 6, 2, 3, 4, 7, 8]
K1 = 3
print("Output for arr1:", can_split_array(arr1, K1)) # Output: True
arr2 = [1, 2, 3, 4, 5]
K2 = 4
print("Output for arr2:", can_split_array(arr2, K2)) # Output: False
C#
using System;
using System.Linq;
public class Program
{
public static bool CanSplitArray(int[] arr, int K)
{
Array.Sort(arr);
for (int i = 0; i < arr.Length - K + 1; i++)
{
if (arr[i + K - 1] - arr[i] != K - 1)
{
return false;
}
}
return true;
}
public static void Main(string[] args)
{
int[] arr1 = { 1, 2, 3, 6, 2, 3, 4, 7, 8 };
int K1 = 3;
Console.WriteLine("Output for arr1: " + CanSplitArray(arr1, K1)); // Output: true
int[] arr2 = { 1, 2, 3, 4, 5 };
int K2 = 4;
Console.WriteLine("Output for arr2: " + CanSplitArray(arr2, K2)); // Output: false
}
}
JavaScript
function canSplitArray(arr, K) {
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length - K + 1; i++) {
if (arr[i + K - 1] - arr[i] !== K - 1) {
return false;
}
}
return true;
}
let arr1 = [1, 2, 3, 6, 2, 3, 4, 7, 8];
let K1 = 3;
console.log("Output for arr1:", canSplitArray(arr1, K1)); // Output: true
let arr2 = [1, 2, 3, 4, 5];
let K2 = 4;
console.log("Output for arr2:", canSplitArray(arr2, K2)); // Output: false
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Similar Reads
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 s
5 min read
Check if N can be divided into K consecutive elements with a sum equal to N Given an integer N, our task is to check if N can be divided into K consecutive elements with a sum equal to N. Print -1 if it is not possible to divide in this manner, otherwise print the value K.Examples: Input: N = 12 Output: 3 Explanation: The integer N = 12 can be divided into 3 consecutive ele
5 min read
Maximise the size of consecutive element subsets in an array Given an integer array and an integer k. The array elements denote positions of points on a 1-D number line, find the maximum size of the subset of points that can have consecutive values of points which can be formed by placing another k points on the number line. Note that all coordinates should b
12 min read
Check if given Array can be divided into subsequences of K increasing consecutive integers Given an array arr[] of N integers and a positive integer K, the task is to check if it is possible to divide the array into increasing subsequences of K consecutive integers such each element can contribute in only a single subsequence. Example: Input: arr[] = {1, 2, 1, 3, 2, 3}, K = 3Output: YesEx
11 min read
Check if an array can be split into K consecutive non-overlapping subarrays of length M consisting of single distinct element Given two integers M and K and an array arr[] consisting of N positive integers, the task is to check if the array can be split into K consecutive non-overlapping subarrays of length M such that each subarray consists of a single distinct element. If found to be true, then print "Yes". Otherwise, pr
8 min read