📌  相关文章
📜  检查是否可以将给定的数组执行任意次数,使给定的数组为0

📅  最后修改于: 2021-05-04 20:23:16             🧑  作者: Mango

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

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

如果给定数组的所有元素都可以变为零,则打印“是”,否则打印“否”

例子:

方法:可以通过奇偶校验解决问题。

  • 由于通过在每次操作中将数组的元素加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


输出:
Yes

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