Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | Function to check if an array can be reduced to single element by removing maximum element among any chosen pairs ; Sort the array in descending order ; Traverse the array ; If the absolute difference of 2 consecutive array elements is greater than K ; If the array can be reduced to a single element ; Driver Code ; Function Call to check if an array can be reduced to a single element
def canReduceArray ( arr , N , K ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i + 1 ] > K ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 1 NEW_LINE canReduceArray ( arr , N , K ) NEW_LINE DEDENT