Check if sum of array can be made equal to X by removing either the first or last digits of every array element
Given an array arr[] consisting of N positive integers and a positive integer X, the task is to check if the sum of the given array can be made equal to X by removing either the first or last digit of every array element. If it is possible to make the sum of array elements equal to X, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[] = {545, 433, 654, 23}, X = 134
Output: Yes
Explanation:
Following removal of digits makes the sum of array equal to X:
- Removing the first digit of arr[0] modifies arr[0] to 45.
- Removing the first digit of arr[1] modifies to 33.
- Removing the first digit of arr[2] modifies arr[2] to 54.
- Removing the last digit of arr[3] modifies arr[3] to 2.
The modified array is {45, 33, 54, 2}. Therefore, sum of the modified array = 45 + 33 + 54 + 2 = 134(= X).
Input: arr[] = {54, 22, 76, 432}, X = 48
Output: No
Approach: The given problem can be solved using recursion by generating all possible ways to remove the first or the last digit for every array element. Follow the steps below to solve the given problem.
- Define a recursive function, say makeSumX(arr, S, i, X) that takes the array arr[], the current sum S, the current index i, and the required sum X as the parameters and perform the following operations:
- If the current index equals the length of the array and the current sum S is equal to the required sum X, then return true. Otherwise, return false.
- Convert the integer arr[i] to a string.
- Remove the last digit of the integer arr[i] and store it in a variable say L.
- Remove the first digit of the integer arr[i] and store it in a variable say R.
- Return the value from the function as the Bitwise OR of makeSumX(arr, S + L, i + 1, X) and makeSumX(arr, S + R, i + 1, X).
- If the value returned by the function makeSumX(arr, 0, 0, X) is true, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // 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 bool makeSumX( int arr[], int X, int S, int i, int N) { // Base Case if (i == N) { return S == X; } // Convert arr[i] to string string a = to_string(arr[i]); // Remove last digit int l = stoi(a.substr(0, a.length() - 1)); // Remove first digit int r = stoi(a.substr(1)); // Recursive function call bool x = makeSumX(arr, X, S + l, i + 1, N); bool y = makeSumX(arr, X, S + r, i + 1, N); return (x || y); } // Function to check if sum of given // array can be made equal to X or not void Check( int arr[], int X, int N) { if (makeSumX(arr, X, 0, 0, N)) { cout << "Yes" ; } else { cout << "No" ; } } // Driver code int main() { int arr[] = { 545, 433, 654, 23 }; int N = sizeof (arr) / sizeof (arr[0]); int X = 134; // Function Call Check(arr, X, N); return 0; } // This code is contributed by Kingash |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG{ // 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 static boolean makeSumX( int arr[], int X, int S, int i) { // Base Case if (i == arr.length) { return S == X; } // Convert arr[i] to string String a = Integer.toString(arr[i]); // Remove last digit int l = Integer.parseInt( a.substring( 0 , a.length() - 1 )); // Remove first digit int r = Integer.parseInt(a.substring( 1 )); // Recursive function call boolean x = makeSumX(arr, X, S + l, i + 1 ); boolean y = makeSumX(arr, X, S + r, i + 1 ); return (x || y); } // Function to check if sum of given // array can be made equal to X or not static void Check( int arr[], int X) { if (makeSumX(arr, X, 0 , 0 )) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } // Driver Code public static void main(String[] args) { int arr[] = { 545 , 433 , 654 , 23 }; int X = 134 ; // Function Call Check(arr, X); } } // This code is contributed by Kingash |
Python3
# Python3 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 def makeSumX(arr, X, S, i): # Base Case if i = = len (arr): return S = = X # Convert arr[i] to string a = str (arr[i]) # Remove last digit l = int (a[: - 1 ]) # Remove first digit r = int (a[ 1 :]) # Recursive function call x = makeSumX(arr, X, S + l, i + 1 ) y = makeSumX(arr, X, S + r, i + 1 ) return x | y # Function to check if sum of given # array can be made equal to X or not def Check(arr, X): if (makeSumX(arr, X, 0 , 0 )): print ( "Yes" ) else : print ( "No" ) # Driver Code arr = [ 545 , 433 , 654 , 23 ] X = 134 Check(arr, X) |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // 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 static bool makeSumX( int [] arr, int X, int S, int i) { // Base Case if (i == arr.Length) { return S == X; } // Convert arr[i] to string string a = Convert.ToString(arr[i]); // Remove last digit int l = Int32.Parse( a.Substring(0, a.Length - 1)); // Remove first digit int r = Int32.Parse(a.Substring(1)); // Recursive function call bool x = makeSumX(arr, X, S + l, i + 1); bool y = makeSumX(arr, X, S + r, i + 1); return (x || y); } // Function to check if sum of given // array can be made equal to X or not static void Check( int [] arr, int X) { if (makeSumX(arr, X, 0, 0)) { Console.Write( "Yes" ); } else { Console.Write( "No" ); } } // Driver Code public static void Main() { int [] arr = { 545, 433, 654, 23 }; int X = 134; // Function Call Check(arr, X); } } // This code is contributed by sanjoy_62 |
Javascript
<script> // Javascript 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 function makeSumX(arr, X, S, i, N) { // Base Case if (i === N) { return S === X } // Convert arr[i] to string let a = (arr[i]).toString() // Remove last digit let l = parseInt((a.substr(0, a.length - 1))) // Remove first digit let r = parseInt((a.substr(1))) // Recursive function call let x = makeSumX(arr, X, S + l, i + 1, N); let y = makeSumX(arr, X, S + r, i + 1, N); return (x || y); } // Function to check if sum of given // array can be made equal to X or not function Check(arr, X, N) { if (makeSumX(arr, X, 0, 0, N)) { document.write( "Yes" ) } else { document.write( "No" ) } } // Driver code let arr = [545, 433, 654, 23]; let N = arr.length; let X = 134; // Function Call Check(arr, X, N); // This code is contributed by Hritik </script> |
Yes
Time Complexity: O(2N)
Auxiliary Space: O(1)
Please Login to comment...