Check if sum of array can be made equal to X by removing either the first or last digits of every array element | Java 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
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean makeSumX ( int arr [ ] , int X , int S , int i ) { if ( i == arr . length ) { return S == X ; } String a = Integer . toString ( arr [ i ] ) ; int l = Integer . parseInt ( a . substring ( 0 , a . length ( ) - 1 ) ) ; int r = Integer . parseInt ( a . substring ( 1 ) ) ; boolean x = makeSumX ( arr , X , S + l , i + 1 ) ; boolean y = makeSumX ( arr , X , S + r , i + 1 ) ; return ( x y ) ; } static void Check ( int arr [ ] , int X ) { if ( makeSumX ( arr , X , 0 , 0 ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 545 , 433 , 654 , 23 } ; int X = 134 ; Check ( arr , X ) ; } }