Check if sum of array can be made equal to X by removing either the first or last digits of every array element | Utility Function to check if the sum of the array elements can be made equal to X by removing either the first or last digits of every array element ; Base Case ; Convert arr [ i ] to string ; Remove last digit ; Remove first digit ; Recursive function call ; Function to check if sum of given array can be made equal to X or not ; Driver code ; Function Call
function makeSumX ( arr , X , S , i , N ) { if ( i === N ) { return S === X } let a = ( arr [ i ] ) . toString ( ) let l = parseInt ( ( a . substr ( 0 , a . length - 1 ) ) ) let r = parseInt ( ( a . substr ( 1 ) ) ) let x = makeSumX ( arr , X , S + l , i + 1 , N ) ; let y = makeSumX ( arr , X , S + r , i + 1 , N ) ; return ( x y ) ; } function Check ( arr , X , N ) { if ( makeSumX ( arr , X , 0 , 0 , N ) ) { document . write ( " " ) } else { document . write ( " " ) } } let arr = [ 545 , 433 , 654 , 23 ] ; let N = arr . length ; let X = 134 ; Check ( arr , X , N ) ;