Split array into minimum number of subsets having maximum pair sum at most K | 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
def cntMinSub ( arr , N , K ) : NEW_LINE INDENT res = 0 NEW_LINE start = 0 NEW_LINE end = N - 1 NEW_LINE arr = sorted ( arr ) NEW_LINE while ( end - start > 1 ) : NEW_LINE INDENT if ( arr [ start ] + arr [ end ] <= K ) : NEW_LINE INDENT start += 1 NEW_LINE DEDENT else : NEW_LINE INDENT res += 1 NEW_LINE end -= 1 NEW_LINE DEDENT DEDENT if ( end - start == 1 ) : NEW_LINE INDENT if ( arr [ start ] + arr [ end ] <= K ) : NEW_LINE INDENT res += 1 NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT res += 1 NEW_LINE end -= 1 NEW_LINE DEDENT DEDENT if ( start == end ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 6 , 8 , 10 , 20 , 25 ] NEW_LINE N = len ( arr ) NEW_LINE K = 26 NEW_LINE print ( cntMinSub ( arr , N , K ) ) NEW_LINE DEDENT