Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | C # 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 ; Driver Code ; Function Call to check if an array can be reduced to a single element
using System ; class GFG { static void canReduceArray ( int [ ] arr , int N , int K ) { Array . 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 ) { Console . WriteLine ( " No " ) ; return ; } } Console . WriteLine ( " Yes " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 1 , 3 } ; int N = arr . Length ; int K = 1 ; canReduceArray ( arr , N , K ) ; } }