📜  通过用数组的总和替换数组元素,将数组修改为另一个给定的数组

📅  最后修改于: 2021-09-04 09:36:00             🧑  作者: Mango

给定的阵列输入[]仅由1秒最初和大小为N的一个阵列目标[]中,任务是检查,如果数组输入[]可以通过用的总和代替输入[I]被转换为目标[]每个步骤中的数组元素。如果发现是真的,则打印“YES” 。否则,打印“NO”

例子:

方法:该问题可以使用贪心技术解决。这个想法是总是通过剩余数组元素的总和减少target[]数组的最大元素,并检查target[]的最大元素。如果发现是真的,则打印“YES” 。否则,打印“NO” 。以下是观察结果:

  • 如果数组target[] 中的最大元素小于1 ,则打印“NO”
  • 如果数组target[] 中的最大元素等于1 ,则打印“YES”
  • 否则,当数组的最大元素大于1 时,将数组target[] 中的最大元素减去数组target[]中剩余元素的总和。

下面是上述方法的实现:

C++
// CPP program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if the arr[] can be
// converted to target[] by replacing
// any element in arr[] by the sum of arr[]
bool isPossible(int target[], int n)
{
 
  // Store the maximum element
  int max = 0;
 
  // Store the index of
  // the maximum element
  int index = 0;
 
  // Traverse the array target[]
  for (int i = 0; i < n; i++) {
 
    // If current element is
    // greater than max
    if (max < target[i]) {
      max = target[i];
      index = i;
    }
  }
 
  // If max element is 1
  if (max == 1)
    return true;
 
  // Traverse the array, target[]
  for (int i = 0; i < n; i++) {
 
    // If current index is not equal to
    // maximum element index
    if (i != index) {
 
      // Update max
      max -= target[i];
 
      // If max is less than
      // or equal to 0,
      if (max <= 0)
        return false;
    }
  }
 
  // Update the maximum element
  target[index] = max;
 
  // Recursively call the function
  return isPossible(target,n);
}
 
// Driver Code
int main()
{
  int target[] = { 9, 3, 5 };
   
  // Size of the array
   int n = sizeof(target) / sizeof(target[0]);
 
  bool res = isPossible(target,n);
 
  if (res)
  {
 
    cout << "YES";
  }
  else
  {
    cout << "NO";
  }
  return 0;
}
 
// This code is contributed by 29AjayKumar


Java
// Java program to implement
// the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to check if the arr[] can be
    // converted to target[] by replacing
    // any element in arr[] by the sum of arr[]
    public static boolean isPossible(int[] target)
    {
 
        // Store the maximum element
        int max = 0;
 
        // Store the index of
        // the maximum element
        int index = 0;
 
        // Traverse the array target[]
        for (int i = 0; i < target.length; i++) {
 
            // If current element is
            // greater than max
            if (max < target[i]) {
                max = target[i];
                index = i;
            }
        }
 
        // If max element is 1
        if (max == 1)
            return true;
 
        // Traverse the array, target[]
        for (int i = 0; i < target.length; i++) {
 
            // If current index is not equal to
            // maximum element index
            if (i != index) {
 
                // Update max
                max -= target[i];
 
                // If max is less than
                // or equal to 0,
                if (max <= 0)
                    return false;
            }
        }
 
        // Update the maximum element
        target[index] = max;
 
        // Recursively call the function
        return isPossible(target);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] target = { 9, 3, 5 };
 
        boolean res = isPossible(target);
 
        if (res) {
 
            System.out.println("YES");
        }
        else {
            System.out.println("NO");
        }
    }
}


Python3
# Python program to implement the above approach
 
# Function to check if the arr[] can be
# converted to target[] by replacing
# any element in arr[] by the sum of arr[]
def isPossible(target):
   
  # Store the maximum element
  max = 0
 
  # Store the index of
  # the maximum element
  index = 0
 
  # Traverse the array target[]
  for i in range(len(target)):
     
    # If current element is
    # greater than max
    if (max < target[i]):
      max = target[i]
      index = i
 
  # If max element is 1
  if (max == 1):
    return True
 
  # Traverse the array, target[]
  for i in range(len(target)):
     
    # If current index is not equal to
    # maximum element index
    if (i != index):
       
      # Update max
      max -= target[i]
       
      # If max is less than
      # or equal to 0,
      if (max <= 0):
        return False
       
  # Update the maximum element
  target[index] = max
 
  # Recursively call the function
  return isPossible(target)
 
# Driver Code
target = [ 9, 3, 5 ]
res = isPossible(target)
if (res):
  print("YES")
else:
  print("NO")
 
  # This code is contributed by RohitSingh07052.


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to check if the arr[] can be
    // converted to target[] by replacing
    // any element in arr[] by the sum of arr[]
    public static bool isPossible(int[] target)
    {
 
        // Store the maximum element
        int max = 0;
 
        // Store the index of
        // the maximum element
        int index = 0;
 
        // Traverse the array target[]
        for (int i = 0; i < target.Length; i++) {
 
            // If current element is
            // greater than max
            if (max < target[i])
            {
                max = target[i];
                index = i;
            }
        }
 
        // If max element is 1
        if (max == 1)
            return true;
 
        // Traverse the array, target[]
        for (int i = 0; i < target.Length; i++) {
 
            // If current index is not equal to
            // maximum element index
            if (i != index) {
 
                // Update max
                max -= target[i];
 
                // If max is less than
                // or equal to 0,
                if (max <= 0)
                    return false;
            }
        }
 
        // Update the maximum element
        target[index] = max;
 
        // Recursively call the function
        return isPossible(target);
    }
 
   
// Driver Code
static public void Main()
{
        int[] target = { 9, 3, 5 };
 
        bool res = isPossible(target);
 
        if (res)
        {
            Console.WriteLine("YES");
        }
        else
        {
            Console.WriteLine("NO");
        }
    }
}
 
// This code is contributed by jana_sayantan.


Javascript


输出:
YES

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live