Split Array into min number of subsets with difference between each pair greater than 1 | Function to split the array into minimum number of subsets with difference strictly > 1 ; Sort the array ; Traverse through the sorted array ; Check the pairs of elements with difference 1 ; If we find even a single pair with difference equal to 1 , then 2 partitions else only 1 partition ; Prlet the count of partitions ; Given array ; Size of the array ; Function call
function split ( arr , n ) { arr . sort ( ) ; let count = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] == 1 ) { count = 2 ; break ; } } document . write ( count ) ; } let arr = [ 2 , 4 , 6 ] ; let n = arr . length ; split ( arr , n ) ;