text: Modify array to another given array by replacing array elements with the sum of the array | CPP program to implement the above approach ; Function to check if the arr [ ] can be converted to target [ ] by replacing any element in arr [ ] by the sum of arr [ ] ; Store the maximum element ; Store the index of the maximum element ; Traverse the array target [ ] ; If current element is greater than max ; If max element is 1 ; Traverse the array , target [ ] ; If current index is not equal to maximum element index ; Update max ; If max is less than or equal to 0 , ; Update the maximum element ; Recursively call the function ; Driver Code ; Size of the array
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int target [ ] , int n ) { int max = 0 ; int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < target [ i ] ) { max = target [ i ] ; index = i ; } } if ( max == 1 ) return true ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != index ) { max -= target [ i ] ; if ( max <= 0 ) return false ; } } target [ index ] = max ; return isPossible ( target , n ) ; } int main ( ) { int target [ ] = { 9 , 3 , 5 } ; int n = sizeof ( target ) / sizeof ( target [ 0 ] ) ; bool res = isPossible ( target , n ) ; if ( res ) { cout << " YES " ; } else { cout << " NO " ; } return 0 ; }