Split Array into min number of subsets with difference between each pair greater than 1 | Java program for the above approach ; 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 ; Print the count of partitions ; Driver Code ; Given array ; Size of the array ; Function call
import java . util . * ; class GFG { static void split ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] == 1 ) { count = 2 ; break ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 } ; int n = arr . length ; split ( arr , n ) ; } }