Check if an array can be split into subsets of K consecutive elements | C # program to implement the above approach ; Function to check if a given array can be split into subsets of K consecutive elements ; Stores the frequencies of array elements ; Traverse the map ; Check if all its occurrences can be grouped into K subsets ; Traverse next K elements ; If the element is not present in the array ; If it cannot be split into required number of subsets ; Driver Code
using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static bool groupInKConsecutive ( int [ ] arr , int K ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; foreach ( int h in arr ) { if ( count . ContainsKey ( h ) ) count [ h ] ++ ; else count [ h ] = 1 ; } foreach ( int c in count . Keys . ToList ( ) ) { int cur = c ; int n = count ; if ( n > 0 ) { for ( int i = 1 ; i < K ; ++ i ) { if ( ! count . ContainsKey ( cur + i ) ) { return false ; } count [ cur + i ] -= n ; if ( count [ cur + i ] < 0 ) return false ; } } } return true ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 6 , 2 , 3 , 4 , 7 , 8 } ; int k = 3 ; if ( groupInKConsecutive ( arr , k ) ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }