Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | Java program for the above approach ; 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 ; Driven Code ; Function Call to check if an array can be reduced to a single element
import java . util . * ; class GFG { static void canReduceArray ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int b [ ] = new int [ N ] ; int j = N ; for ( int i = 0 ; i < N ; i ++ ) { b [ j - 1 ] = arr [ i ] ; j = j - 1 ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { System . out . print ( " No " ) ; return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 3 } ; int N = arr . length ; int K = 1 ; canReduceArray ( arr , N , K ) ; } }