Modify array to another given array by replacing array elements with the sum of the array | 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
function isPossible ( target ) { var max = 0 ; var index = 0 ; for ( i = 0 ; i < target . length ; i ++ ) { if ( max < target [ i ] ) { max = target [ i ] ; index = i ; } } if ( max == 1 ) return true ; for ( i = 0 ; i < target . length ; i ++ ) { if ( i != index ) { max -= target [ i ] ; if ( max <= 0 ) return false ; } } target [ index ] = max ; return isPossible ( target ) ; } var target = [ 9 , 3 , 5 ] ; res = isPossible ( target ) ; if ( res ) { document . write ( " " ) ; } else { document . write ( " " ) ; }