Check if sum of array can be made equal to X by removing either the first or last digits of every array element
Last Updated :
04 Apr, 2023
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 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++
#include <bits/stdc++.h>
using namespace std;
bool makeSumX( int arr[], int X, int S,
int i, int N)
{
if (i == N)
{
return S == X;
}
string a = to_string(arr[i]);
int l = stoi(a.substr(0, a.length() - 1));
int r = stoi(a.substr(1));
bool x = makeSumX(arr, X, S + l, i + 1, N);
bool y = makeSumX(arr, X, S + r, i + 1, N);
return (x || y);
}
void Check( int arr[], int X, int N)
{
if (makeSumX(arr, X, 0, 0, N))
{
cout << "Yes" ;
}
else
{
cout << "No" ;
}
}
int main()
{
int arr[] = { 545, 433, 654, 23 };
int N = sizeof (arr) / sizeof (arr[0]);
int X = 134;
Check(arr, X, N);
return 0;
}
|
Java
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);
}
}
|
Python3
def makeSumX(arr, X, S, i):
if i = = len (arr):
return S = = X
a = str (arr[i])
l = int (a[: - 1 ])
r = int (a[ 1 :])
x = makeSumX(arr, X, S + l, i + 1 )
y = makeSumX(arr, X, S + r, i + 1 )
return x | y
def Check(arr, X):
if (makeSumX(arr, X, 0 , 0 )):
print ( "Yes" )
else :
print ( "No" )
arr = [ 545 , 433 , 654 , 23 ]
X = 134
Check(arr, X)
|
C#
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);
}
}
|
Javascript
<script>
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( "Yes" )
}
else {
document.write( "No" )
}
}
let arr = [545, 433, 654, 23];
let N = arr.length;
let X = 134;
Check(arr, X, N);
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...