Split array into minimum number of subsets having maximum pair sum at most K | Java program to implement the above approach ; Function to get the minimum count of subsets that satisfy the given condition ; Store the minimum count of subsets that satisfy the given condition ; Stores start index of the sorted array . ; Stores end index of the sorted array ; Sort the given array ; Traverse the array ; If only two elements of sorted array left ; If only one elements left in the array ; Driver Code
import java . util . * ; class GFG { static int cntMinSub ( int arr [ ] , int N , int K ) { int res = 0 ; int start = 0 ; int end = N - 1 ; Arrays . sort ( arr ) ; while ( end - start > 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { start ++ ; } else { res ++ ; end -- ; } } if ( end - start == 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { res ++ ; start ++ ; end -- ; } else { res ++ ; end -- ; } } if ( start == end ) { res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 8 , 10 , 20 , 25 } ; int N = arr . length ; int K = 26 ; System . out . print ( cntMinSub ( arr , N , K ) ) ; } }