📌  相关文章
📜  检查给定的数组是否可以通过执行任意次数的给定操作变为 0

📅  最后修改于: 2021-09-02 07:34:49             🧑  作者: Mango

给定一个包含N 个整数的数组arr[] ,任务是通过以下操作找出给定数组的所有元素是否都可以为 0:

  • 将任何元素增加 2。
  • 从数组中的所有元素中减去数组的最小元素。
  • 上述操作可以执行任意次数。

如果给定数组的所有元素都可以为零,则打印Yes否则打印No
例子:

处理方法:问题可以借助 Parity 解决。

  • 因为,通过在每次操作中将数组的元素增加 2,元素的奇偶校验不会改变,即奇数仍然是奇数或偶数仍然是偶数
  • 并且在用数组中的最小元素减去数组的每个元素后,偶数的奇偶校验变为奇数,奇数的奇偶校验变为偶数
  • 因此,要使数组的所有元素为 0,所有元素的奇偶校验必须相同,否则我们无法通过给定的操作使数组的所有元素为 0。

下面是上述方法的实现:

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
// Function to whether the array
// can be made zero or not
bool check(int arr[], int N)
{
    // Count for even elements
    int even = 0;
 
    // Count for odd elements
    int odd = 0;
 
    // Traverse the array to
    // count the even and odd
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is odd
        if (arr[i] & 1) {
            odd++;
        }
 
        // If arr[i] is even
        else {
            even++;
        }
    }
 
    // Check if count of even
    // is zero or count of odd
    // is zero
    if (even == N || odd == N)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver's Code
int main()
{
    int arr[] = { 1, 1, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    check(arr, N);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG{
  
// Function to whether the array
// can be made zero or not
static void check(int arr[], int N)
{
    // Count for even elements
    int even = 0;
  
    // Count for odd elements
    int odd = 0;
  
    // Traverse the array to
    // count the even and odd
    for (int i = 0; i < N; i++) {
  
        // If arr[i] is odd
        if (arr[i] % 2 == 1) {
            odd++;
        }
  
        // If arr[i] is even
        else {
            even++;
        }
    }
  
    // Check if count of even
    // is zero or count of odd
    // is zero
    if (even == N || odd == N)
        System.out.print("Yes");
    else
        System.out.print("No");
}
  
// Driver's Code
public static void main(String[] args)
{
    int arr[] = { 1, 1, 3 };
    int N = arr.length;
  
    check(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to whether the array
# can be made zero or not
def check(arr, N):
     
    # Count for even elements
    even = 0;
 
    # Count for odd elements
    odd = 0;
 
    # Traverse the array to
    # count the even and odd
    for i in range(N):
 
        # If arr[i] is odd
        if (arr[i] % 2 == 1):
            odd += 1;
     
        # If arr[i] is even
        else:
            even += 1;
 
    # Check if count of even
    # is zero or count of odd
    # is zero
    if (even == N or odd == N):
        print("Yes");
    else:
        print("No");
 
# Driver's Code
if __name__ == '__main__':
    arr = [ 1, 1, 3];
    N = len(arr);
 
    check(arr, N);
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
 
class GFG{
   
// Function to whether the array
// can be made zero or not
static void check(int []arr, int N)
{
    // Count for even elements
    int even = 0;
   
    // Count for odd elements
    int odd = 0;
   
    // Traverse the array to
    // count the even and odd
    for (int i = 0; i < N; i++) {
   
        // If arr[i] is odd
        if (arr[i] % 2 == 1) {
            odd++;
        }
   
        // If arr[i] is even
        else {
            even++;
        }
    }
   
    // Check if count of even
    // is zero or count of odd
    // is zero
    if (even == N || odd == N)
        Console.Write("Yes");
    else
        Console.Write("No");
}
   
// Driver's Code
public static void Main(String[] args)
{
    int []arr = { 1, 1, 3 };
    int N = arr.Length;
   
    check(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Yes

时间复杂度: O(N) ,其中 N 是给定数组的长度。