Check if sum of array can be made equal to X by removing either the first or last digits of every array element | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static bool makeSumX ( int [ ] arr , int X , int S , int i ) { if ( i == arr . Length ) { return S == X ; } string a = Convert . ToString ( arr [ i ] ) ; int l = Int32 . Parse ( a . Substring ( 0 , a . Length - 1 ) ) ; int r = Int32 . Parse ( a . Substring ( 1 ) ) ; bool x = makeSumX ( arr , X , S + l , i + 1 ) ; bool y = makeSumX ( arr , X , S + r , i + 1 ) ; return ( x y ) ; } static void Check ( int [ ] arr , int X ) { if ( makeSumX ( arr , X , 0 , 0 ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { int [ ] arr = { 545 , 433 , 654 , 23 } ; int X = 134 ; Check ( arr , X ) ; } }