📌  相关文章
📜  通过删除每个数组元素的第一位或最后一位来检查数组的总和是否等于X

📅  最后修改于: 2021-04-17 15:14:01             🧑  作者: Mango

给定一个由N个正整数和一个正整数X组成的数组arr [] ,任务是检查是否可以通过删除每个数组元素的第一个或最后一个数字来使给定数组的总和等于X。如果可以使数组元素的总和等于X ,则打印“是” 。否则,打印“否”

例子:

方法:可以使用递归方法解决给定的问题,方法是生成所有可能的方法以删除每个数组元素的第一位或最后一位数字。请按照以下步骤解决给定的问题。

  • 定义一个递归函数,例如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)返回的值为true,则打印“ 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


输出:
Yes

时间复杂度: O(2 N )
辅助空间: O(1)