📌  相关文章
📜  检查是否可以使用 K 三元组增量操作使数组总和等于 X

📅  最后修改于: 2022-05-13 01:56:07.506000             🧑  作者: Mango

检查是否可以使用 K 三元组增量操作使数组总和等于 X

给定一个长度为N的数组arr和两个整数KX。对于数组中的每个元素arr i ,如果arr i > 0 ,那么对于所有1≤i ,与其相邻的元素和元素本身都将增加 2 ≤N 。检查执行K运算后所有数组元素的总和是否等于X。

例子:

方法:这个问题可以使用蛮力打击和试验技术来解决:

  • 遍历给定的数组
  • 对于每个索引,将该索引及其相邻索引处的值增加 2,但第一个和最后一个元素除外,因为它们只有一个相邻的元素。
  • 对给定数组中的每种可能性重复操作,直到数组总和最多变为 X
  • 如果经过 K 次操作,数组和有任何可能变为 X,则返回 true。
  • 如果不存在这种可能性,则返回 false。

下面是上述方法的实现:

C++
// C++ program to check whether sum
// Is equal to target value
// After K operations
#include 
using namespace std;
 
// Function to check sum of array
// Value is equal to Target
bool find(int arr[], int N, int K, int Target)
{
    while (K--) {
        for (int i = 0; i < N; i++) {
            // If it is first element just increment
            // next element
            if (arr[i] > 0 && i == 0) {
                arr[i + 1] = arr[i + 1] + 2;
            }
            // If it is last element just increment
            // Last second element
            else if (arr[i] > 0 && i == N - 1) {
                arr[N - 2] = arr[N - 2] + 2;
            }
            // If it is not a first element
            // Or not a last element
            // Increment both adjacent value by 2
 
            else if (arr[i] > 0) {
                arr[i - 1] = arr[i - 1] + 2;
                arr[i + 1] = arr[i + 1] + 2;
            }
        }
    }
    // Calculate sum after performing
    // k times operation
    int ans = accumulate(arr, arr + N, 0);
    if (ans == Target) {
        return 1;
    }
    else {
        return 0;
    }
}
// Driver Code
int main()
{
    int arr[] = { 0, 0, 1, 0, 0, 3 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 2;
    int Target = 36;
 
    // function call
    cout << (find(arr, N, K, Target)
                 ? "true"
                 : "false");
    return 0;
}


Java
// JAVA program to check whether sum
// Is equal to target value
// After K operations
import java.util.*;
class GFG
{
 
  // Function to check sum of array
  // Value is equal to Target
  public static boolean find(int arr[], int N, int K,
                             int Target)
  {
    while ((K--) != 0) {
      for (int i = 0; i < N; i++)
      {
 
        // If it is first element just increment
        // next element
        if (arr[i] > 0 && i == 0) {
          arr[i + 1] = arr[i + 1] + 2;
        }
 
        // If it is last element just increment
        // Last second element
        else if (arr[i] > 0 && i == N - 1) {
          arr[N - 2] = arr[N - 2] + 2;
        }
 
        // If it is not a first element
        // Or not a last element
        // Increment both adjacent value by 2
        else if (arr[i] > 0) {
          arr[i - 1] = arr[i - 1] + 2;
          arr[i + 1] = arr[i + 1] + 2;
        }
      }
    }
 
    // Calculate sum after performing
    // k times operation
    int ans = 0;
    for (int i = 0; i < arr.length; i++) {
      ans += arr[i];
    }
 
    if (ans == Target) {
      return true;
    }
    else {
      return false;
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = new int[] { 0, 0, 1, 0, 0, 3 };
 
    int N = arr.length;
    int K = 2;
    int Target = 36;
 
    // function call
    System.out.print(
      (find(arr, N, K, Target) ? "true" : "false"));
  }
}
 
// This code is contributed by Taranpreet


Python3
# Function to check sum of array
# Value is equal to Target
def find(arr, N, K, Target) :
     
    for j in range(K, 1, -1):
        for i in range(0, N):
            # If it is first element just increment
            # next element
            if (arr[i] > 0 and i == 0) :
                arr[i + 1] = arr[i + 1] + 2
             
            # If it is last element just increment
            # Last second element
            elif (arr[i] > 0 and i == N - 1) :
                arr[N - 2] = arr[N - 2] + 2
             
            # If it is not a first element
            # Or not a last element
            # Increment both adjacent value by 2
 
            elif (arr[i] > 0) :
                arr[i - 1] = arr[i - 1] + 2
                arr[i + 1] = arr[i + 1] + 2
             
         
     
    # Calculate sum after performing
    # k times operation
    ans = 0
    for i in range(0, len(arr)):
        ans += arr[i]
         
    if (ans == Target) :
        return 1
     
    else :
        return 0
     
# Driver Code
arr = [ 0, 0, 1, 0, 0, 3 ]
 
N = len(arr)
K = 2
Target = 36
 
# function call
print ( "false" if find(arr, N, K, Target) else "true" )
 
# This code is contributed by sanjoy_62.


C#
// C# program to check whether sum
// Is equal to target value
// After K operations
using System;
 
public class GFG{
 
  // Function to check sum of array
  // Value is equal to Target
  static bool find(int[] arr, int N, int K,
                   int Target)
  {
    while ((K--) != 0) {
      for (int i = 0; i < N; i++)
      {
 
        // If it is first element just increment
        // next element
        if (arr[i] > 0 && i == 0) {
          arr[i + 1] = arr[i + 1] + 2;
        }
 
        // If it is last element just increment
        // Last second element
        else if (arr[i] > 0 && i == N - 1) {
          arr[N - 2] = arr[N - 2] + 2;
        }
 
        // If it is not a first element
        // Or not a last element
        // Increment both adjacent value by 2
        else if (arr[i] > 0) {
          arr[i - 1] = arr[i - 1] + 2;
          arr[i + 1] = arr[i + 1] + 2;
        }
      }
    }
 
    // Calculate sum after performing
    // k times operation
    int ans = 0;
    for (int i = 0; i < arr.Length; i++) {
      ans += arr[i];
    }
 
    if (ans == Target) {
      return true;
    }
    else {
      return false;
    }
  }
 
  // Driver Code
  static public void Main (){
 
    int[] arr = { 0, 0, 1, 0, 0, 3 };
 
    int N = arr.Length;
    int K = 2;
    int Target = 36;
 
    // function call
    Console.Write(
      (find(arr, N, K, Target) ? "true" : "false"));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript


输出
true

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