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

📅  最后修改于: 2021-04-21 21:52:10             🧑  作者: Mango

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

例子:

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

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

下面是上述方法的实现:

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)