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

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 = 4Output:falseExplanation:

The given array of length 5 cannot be split into subsets of 4.

**Approach**

Follow the steps to solve the problem:

- Store the frequencies of all array elements in a HashMap
- Traverse the
**HashMap**. - For every element present in the
**HashMap**, check if all its occurrences can be grouped in a subsets with its**next (K – 1) consecutive elements**or not. If so, reduce the frequencies of the elements included in the subsets accordingly in the**HashMap**and proceed forward. - If any element is found which cannot be grouped into a subset of K consecutive elements, print False. Otherwise print True.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement the` `// above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check if a given array can` `// be split into subsets of K consecutive` `// elements` `bool` `groupInKConsecutive(vector<` `int` `>& arr,` ` ` `int` `K)` `{` ` ` `// Stores the frequencies of` ` ` `// array elements` ` ` `map<` `int` `, ` `int` `> count;` ` ` `for` `(` `int` `h : arr) {` ` ` `++count[h];` ` ` `}` ` ` `// Traverse the map` ` ` `for` `(` `auto` `c : count) {` ` ` `int` `cur = c.first;` ` ` `int` `n = c.second;` ` ` `// Check if all its occurrences can` ` ` `// be grouped into K subsets` ` ` `if` `(n > 0) {` ` ` `// Traverse next K elements` ` ` `for` `(` `int` `i = 1; i < K; ++i) {` ` ` `// If the element is not` ` ` `// present in the array` ` ` `if` `(!count.count(cur + i)) {` ` ` `return` `false` `;` ` ` `}` ` ` `count[cur + i] -= n;` ` ` `// If it cannot be split into` ` ` `// required number of subsets` ` ` `if` `(count[cur + i] < 0)` ` ` `return` `false` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `return` `true` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `vector<` `int` `> arr = { 1, 2, 3, 6, 2,` ` ` `3, 4, 7, 8 };` ` ` `int` `k = 3;` ` ` `if` `(groupInKConsecutive(arr, k)) {` ` ` `cout << ` `"True"` `;` ` ` `}` ` ` `else` `{` ` ` `cout << ` `"False"` `;` ` ` `}` `}` |

*chevron_right*

*filter_none*

## Java

`// Java Program to implement the` `// above approach` `import` `java.util.*;` `class` `GFG{` `// Function to check if a given array can` `// be split into subsets of K consecutive` `// elements` `static` `boolean` `groupInKConsecutive(` `int` `[] arr,` ` ` `int` `K)` `{` ` ` `// Stores the frequencies of` ` ` `// array elements` ` ` `HashMap<Integer,` ` ` `Integer> count = ` `new` `HashMap<Integer,` ` ` `Integer>();` ` ` `for` `(` `int` `h : arr) ` ` ` `{` ` ` `if` `(count.containsKey(h))` ` ` `count.put(h, count.get(h) + ` `1` `);` ` ` `else` ` ` `count.put(h, ` `1` `);` ` ` `}` ` ` `// Traverse the map` ` ` `for` `(Map.Entry<Integer,` ` ` `Integer> c : count.entrySet())` ` ` `{` ` ` `int` `cur = c.getKey();` ` ` `int` `n = c.getValue();` ` ` `// Check if all its occurrences can` ` ` `// be grouped into K subsets` ` ` `if` `(n > ` `0` `)` ` ` `{` ` ` `// Traverse next K elements` ` ` `for` `(` `int` `i = ` `1` `; i < K; ++i)` ` ` `{` ` ` `// If the element is not` ` ` `// present in the array` ` ` `if` `(!count.containsKey(cur + i)) ` ` ` `{` ` ` `return` `false` `;` ` ` `}` ` ` `count.put(cur + i, count.get(cur + i) - n);` ` ` `// If it cannot be split into` ` ` `// required number of subsets` ` ` `if` `(count.get(cur + i) < ` `0` `)` ` ` `return` `false` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `return` `true` `;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `[] arr = { ` `1` `, ` `2` `, ` `3` `, ` `6` `, ` `2` `,` ` ` `3` `, ` `4` `, ` `7` `, ` `8` `};` ` ` `int` `k = ` `3` `;` ` ` `if` `(groupInKConsecutive(arr, k))` ` ` `{` ` ` `System.out.print(` `"True"` `);` ` ` `}` ` ` `else` ` ` `{` ` ` `System.out.print(` `"False"` `);` ` ` `}` `}` `}` `// This code contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement the` `# above approach` `from` `collections ` `import` `defaultdict` `# Function to check if a given array can` `# be split into subsets of K consecutive` `# elements` `def` `groupInKConsecutive(arr, K):` ` ` `# Stores the frequencies of` ` ` `# array elements` ` ` `count ` `=` `defaultdict(` `int` `)` ` ` `for` `h ` `in` `arr:` ` ` `count[h] ` `+` `=` `1` ` ` `# Traverse the map` ` ` `for` `key, value ` `in` `count.items():` ` ` `cur ` `=` `key` ` ` `n ` `=` `value` ` ` `# Check if all its occurrences can` ` ` `# be grouped into K subsets` ` ` `if` `(n > ` `0` `):` ` ` `# Traverse next K elements` ` ` `for` `i ` `in` `range` `(` `1` `, K):` ` ` `# If the element is not` ` ` `# present in the array` ` ` `if` `((cur ` `+` `i) ` `not` `in` `count):` ` ` `return` `False` ` ` `count[cur ` `+` `i] ` `-` `=` `n` ` ` `# If it cannot be split into` ` ` `# required number of subsets` ` ` `if` `(count[cur ` `+` `i] < ` `0` `):` ` ` `return` `False` ` ` ` ` `return` `True` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `6` `, ` `2` `,` ` ` `3` `, ` `4` `, ` `7` `, ` `8` `]` ` ` `k ` `=` `3` ` ` ` ` `if` `(groupInKConsecutive(arr, k)):` ` ` `print` `(` `"True"` `)` ` ` `else` `:` ` ` `print` `(` `"False"` `)` `# This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

**Output:**

True

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

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 an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Check if it is possible to split given Array into K odd-sum subsets
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Split array into K subsets to maximize their sum of maximums and minimums
- Split Array into min number of subsets with difference between each pair greater than 1
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Check if an array can be split into subarrays with GCD exceeding K
- Check if an array can be split into 3 subsequences of equal sum or not
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Split numbers from 1 to N into two equal sum subsets
- Check whether an array can be fit into another array rearranging the elements in the array
- Check if a string can be split into two strings with same number of K-frequent characters
- Split an array containing N elements into K sets of distinct elements
- Count array elements that can be represented as sum of at least two consecutive array elements
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Check if elements of an array can be arranged in a Circle with consecutive difference as 1

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.