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++
// 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>
Time Complexity: O(2N)
Auxiliary Space: O(N)
Similar Reads
Check if Arrays can be made equal by Replacing elements with their number of Digits Given two arrays A[] and B[] of length N, the task is to check if both arrays can be made equal by performing the following operation at most K times: Choose any index i and either change Ai to the number of digits Ai have or change Bi to the number of digits Bi have. Examples: Input: N = 4, K = 1,
10 min read
Check if Array elements can be made equal by adding unit digit to the number Given an array arr[] of N integers, the task is to check whether it is possible to make all the array elements identical by applying the following option any number of times: Choose any number and add its unit digit to it. Examples: Input: arr[] = {1, 2, 4, 8, 24}Output: PossibleExplanation: For 1:
7 min read
Check if possible to make Array sum equal to Array product by replacing exactly one element Given an array arr[] consisting of N non-negative integers, the task is to check if it is possible to make the sum of the array equal to the product of the array element by replacing exactly one array element with any non-negative integer. Examples: Input: arr[] = {1, 3, 4}Output: YesExplanation:Rep
7 min read
Check if a number can be represented as the sum of numbers with at least one digit equal to K Given integers N and K, the task is to check if a number can be represented as the sum of numbers that have at least one digit equal to K. Example: Input: N = 68, K = 7Output: YESExplanation: 68 = (27 + 17 + 17 + 7). Each number has atleast one digit equal to 7. Input: N = 23, K = 3Output: YESExplan
6 min read
Modify a given array by replacing each element with the sum or product of their digits based on a given condition Given an array arr[] consisting of N integers, the task is to modify the array elements after performing only one of the following operations on each array elements: If the count of even digits is greater than the count of odd digits in an array element, then update that element to the sum of all th
8 min read
Check if the array has an element which is equal to sum of all the remaining elements Given an array of N elements, the task is to check if the array has an element that is equal to the sum of all the remaining elements. Examples: Input: a[] = {5, 1, 2, 2} Output: Yes we can write 5=(1+2+2) Input: a[] = {2, 1, 2, 4, 3} Output: No Approach: Suppose that the total elements in the array
10 min read