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
function canReduceArray ( arr , N , K ) { arr . sort ( ) ; let b = Array ( N ) . fill ( 0 ) ; let j = N ; for ( let i = 0 ; i < N ; i ++ ) { b [ j - 1 ] = arr [ i ] ; j = j - 1 ; } for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let arr = [ 2 , 1 , 1 , 3 ] ; let N = arr . length ; let K = 1 ; canReduceArray ( arr , N , K ) ;