给定一个由N 个正整数和一个正整数X组成的数组arr[] ,任务是通过删除每个数组元素的第一个或最后一个数字来检查是否可以使给定数组的总和等于X。如果可以使数组元素的总和等于X ,则打印“是” 。否则,打印“否” 。
例子:
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
方法:通过生成所有可能的方法来删除每个数组元素的第一个或最后一个数字,可以使用递归解决给定的问题。请按照以下步骤解决给定的问题。
- 定义一个递归函数,比如makeSumX(arr, S, i, X) ,它以数组arr[] 、当前总和S 、当前索引i和所需总和X作为参数并执行以下操作:
- 如果当前索引等于数组的长度并且当前总和S等于所需的总和X ,则返回true 。否则,返回false 。
- 将整数arr[i]转换为字符串。
- 删除整数arr[i]的最后一位数字并将其存储在一个变量中,比如L 。
- 删除整数arr[i]的第一个数字并将其存储在一个变量中,比如R 。
- 将函数的值作为makeSumX(arr, S + L, i + 1, X) 和 makeSumX(arr, S + R, i + 1, X)的按位或返回。
- 如果函数makeSumX(arr, 0, 0, X) 返回的值为真,则打印“Yes” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
Yes
时间复杂度: O(2 N )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live