📌  相关文章
📜  检查给定数组的总和是否可以通过将数组元素减少 K 来减少到 0

📅  最后修改于: 2021-10-26 05:53:27             🧑  作者: Mango

给定一个由N 个整数和一个整数K组成的数组arr[] 任务是检查数组的总和是否可以通过将数组元素减去K任意次来减少到 0。

例子:

处理方法:按照以下步骤解决问题:

  1. 遍历数组并计算给定数组的总和。
  2. 根据sum的值,出现以下情况:
    1. 如果 sum = 0:不需要操作。因此,答案是“是”。
    2. 如果 sum > 0:只有当sumK的倍数时,Sum 才能减少到0 。如果sum不是K的倍数,则打印“No”。否则,打印“是”
    3. 如果 sum < 0:只需打印“No”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the
// sum can be made 0 or not
int sumzero(int arr[], int N, int K)
{
    // Stores sum of array elements
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        sum += arr[i];
    }
 
    if (sum == 0)
        cout << "Yes";
 
    else if (sum > 0) {
 
        if (sum % K == 0)
            cout << "Yes";
 
        else
            cout << "No";
    }
 
    else
        cout << "No";
    return 0;
}
 
// Driver Code
int main()
{
    int K, N;
 
    // Given array arr[]
    int arr1[] = { 1, -6, 2, 2 };
    K = 1;
    N = sizeof(arr1) / sizeof(arr1[0]);
 
    sumzero(arr1, N, K);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG{
 
// Function to check if the
// sum can be made 0 or not
static int sumzero(int arr[], int N, int K)
{
    // Stores sum of array elements
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        sum += arr[i];
    }
 
    if (sum == 0)
        System.out.print("Yes");
 
    else if (sum > 0) {
 
        if (sum % K == 0)
            System.out.print("Yes");
 
        else
            System.out.print("No");
    }
 
    else
        System.out.print("No");
    return 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int K, N;
 
    // Given array arr[]
    int arr1[] = { 1, -6, 2, 2 };
    K = 1;
    N = arr1.length;
 
    sumzero(arr1, N, K);
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to check if the
# sum can be made 0 or not
def sumzero(arr, N, K) :
     
    # Stores sum of array elements
    sum = 0;
 
    # Traverse the array
    for i in range(N) :
        sum += arr[i];
    if (sum == 0) :
        print("Yes");
    elif (sum > 0) :
        if (sum % K == 0) :
            print("Yes");
        else :
            print("No");
    else :
        print("No");
 
# Driver Code
if __name__ == "__main__" :
 
    # Given array arr[]
    arr1 = [ 1, -6, 2, 2 ];
     
    K = 1;
    N = len(arr1);
 
    sumzero(arr1, N, K);
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to check if the
// sum can be made 0 or not
static int sumzero(int []arr, int N, int K)
{
   
    // Stores sum of array elements
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        sum += arr[i];
    }
 
    if (sum == 0)
        Console.Write("Yes");
 
    else if (sum > 0)
    {
 
        if (sum % K == 0)
            Console.Write("Yes");
 
        else
            Console.Write("No");
    }
 
    else
        Console.Write("No");
    return 0;
}
 
// Driver Code
public static void Main(String[] args)
{
    int K, N;
 
    // Given array []arr
    int []arr1 = { 1, -6, 2, 2 };
    K = 1;
    N = arr1.Length;
 
    sumzero(arr1, N, K);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
No

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程