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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canReduceArray ( int arr [ ] , int N , int K ) { sort ( arr , arr + N , greater < int > ( ) ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { cout << " No " ; return ; } } cout << " Yes " ; } int main ( ) { int arr [ ] = { 2 , 1 , 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 1 ; canReduceArray ( arr , N , K ) ; return 0 ; }