📌  相关文章
📜  通过减少相邻对对来制作零数组

📅  最后修改于: 2021-04-27 18:23:28             🧑  作者: Mango

给定一系列非负整数,例如a 1 ,a 2 ,…,a n 。在给定序列上只能执行以下操作:

  • 将a [i]和a [i + 1]都减去1。

查找是否可以使用任意数量的上述运算将序列修改为全零。

例子 :

Input  : 1 2
Output : NO
Explanation: Only two elements, if we subtract
1 then it will convert into [0, 1].so answer is NO. 

Input  : 0 1 1 0
Output : YES
Explanation: Here we can choose both 1 and 
subtract 1 from them then array becomes [0, 0, 0, 
0].So answer is YES.

Input  :  1 2 3 4
Output : NO
Explanation: if we try to subtract 1 any
number of times then array will be [0, 0, 0, 1].
[1, 2, 3, 4]->[0, 1, 3, 4]->[0, 0, 2, 3]->
[0, 0, 1, 2]->[0, 0, 0, 1]. 

方法1:如果数组中所有相邻元素(i,i + 1)相等,并且数组中元素总数为偶数,则可以将所有元素转换为零。例如,如果数组元素类似于{1、1、2、2、3、3},则其所有元素都可以转换为零。

那么在这种情况下,每个奇数位置值的总和总是等于偶数位置值的总和。

C++
// CPP program to find if it is possible
// to make all array elements 0 by decrement
// operations.
#include 
using namespace std;
  
bool isPossibleToZero(int a[], int n)
{
    // used for storing the sum of even 
    // and odd position element in array.
    int even = 0, odd = 0; 
      
    for (int i = 0; i < n; i++) 
    {
        // if position is odd, store sum 
        // value of odd position in odd
        if (i & 1) 
            odd += a[i];
          
        // if position is even, store sum 
        // value of even position in even
        else 
            even += a[i];
    }
      
    return (odd == even);
}
  
// Driver program
int main()
{
    int arr[] = { 0, 1, 1, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    if (isPossibleToZero(arr, n))
        cout << "YES";
    else
        cout << "NO";
}


Java
// Java program to find if
// it is possible to make 
// all array elements 0 by
// decrement operations.
import java.io.*;
  
class GFG 
{
    static boolean isPossibleToZero(int a[],    
                                    int n)
{
    // used for storing the 
    // sum of even and odd 
    // position element in array.
    int even = 0, odd = 0; 
      
    for (int i = 0; i < n; i++) 
    {
        // if position is odd, 
        // store sum value of 
        // odd position in odd
        if ((i & 1) == 0) 
            odd += a[i];
          
        // if position is even, 
        // store sum value of 
        // even position in even
        else
            even += a[i];
    }
      
    return (odd == even);
}
  
// Driver Code
public static void main(String[] args) 
{
    int arr[] = { 0, 1, 1, 0 };
    int n = arr.length;
    if (isPossibleToZero(arr, n))
        System.out.println("YES");
    else
        System.out.println("NO");
}
}
  
// This code is contributed by m_kit


Python3
# Python3 program to find if it is 
# possible to make all array elements
# 0 by decrement operations.
def isPossibleToZero(a, n):
  
    # used for storing the 
    # sum of even and odd 
    # position element in array.
    even = 0; 
    odd = 0; 
      
    for i in range(n): 
          
        # if position is odd, store sum 
        # value of odd position in odd
        if (i & 1): 
            odd += a[i];
          
        # if position is even, store sum 
        # value of even position in even
        else:
            even += a[i];
      
    return (odd == even);
  
# Driver Code
arr = [0, 1, 1, 0];
n = len(arr);
if (isPossibleToZero(arr, n)):
    print("YES");
else:
    print("NO");
  
# This code is contributed by mits


C#
// C# program to find if
// it is possible to make 
// all array elements 0 by
// decrement operations.
using System;
  
class GFG
{
static bool isPossibleToZero(int []a, 
                             int n)
{
    // used for storing the 
    // sum of even and odd 
    // position element in array.
    int even = 0, odd = 0; 
      
    for (int i = 0; i < n; i++) 
    {
        // if position is odd, 
        // store sum value of 
        // odd position in odd
        if ((i & 1) == 0) 
            odd += a[i];
          
        // if position is even, 
        // store sum value of 
        // even position in even
        else
            even += a[i];
    }
      
    return (odd == even);
}
  
// Driver Code
static public void Main ()
{
    int []arr = {0, 1, 1, 0};
    int n = arr.Length;
    if (isPossibleToZero(arr, n))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
  
// This code is contributed
// by m_kit


PHP


C++
// CPP implementation of the 
// above approach
#include 
using namespace std;
  
bool isPossibleToZero(int a[], int n)
{  
    // converting array element into number
    int num = 0;
    for (int i = 0; i < n; i++)
        num = num * 10 + a[i]; 
  
    // Check if divisible by 11
    return (num % 11 == 0);
}
  
// Driver program
int main()
{
    int arr[] = { 0, 1, 1, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    if (isPossibleToZero(arr, n))
        cout << "YES";
    else
        cout << "NO";
}


Java
// Java implementation of the above approach
import java.io.*;
  
class GFG {
      
    static boolean isPossibleToZero(int a[], int n)
    { 
        // converting array element into number
        int num = 0;
        for (int i = 0; i < n; i++)
            num = num * 10 + a[i]; 
      
        // Check if divisible by 11
        return (num % 11 == 0);
    }
      
    // Driver program
    public static void main (String[] args)
    {
  
        int arr[] = {0, 1, 1, 0};
        int n = arr.length;
        if (isPossibleToZero(arr, n))
                System.out.println( "YES");
        else
                System.out.println ("NO");
    }
}
  
// This code is contributed by @ajit


Python3
# Python3 implementation of the 
# above approach
  
def isPossibleToZero(a, n):
      
    # converting array element 
    # into number
    num = 0;
    for i in range(n):
        num = num * 10 + a[i]; 
  
    # Check if divisible by 11
    return (num % 11 == 0);
  
# Driver Code
arr = [ 0, 1, 1, 0 ];
n = len(arr);
if (isPossibleToZero(arr, n)):
    print("YES");
else:
    print("NO");
  
# This code is contributed mits


C#
// C# implementation of the above approach
using System;
  
class GFG 
{
      
static bool isPossibleToZero(int[] a, int n)
{ 
    // converting array element into number
    int num = 0;
    for (int i = 0; i < n; i++)
        num = num * 10 + a[i]; 
  
    // Check if divisible by 11
    return (num % 11 == 0);
}
  
// Driver Code
public static void Main()
{
    int[] arr = {0, 1, 1, 0};
    int n = arr.Length;
    if (isPossibleToZero(arr, n))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
  
// This code is contributed 
// by Akanksha Rai


PHP


输出:
YES

方法2:如果给定数组元素形成的Number可被11整除,则该数组的所有元素也可以转换为零。

例如:给定数组{0,1,1,0},由该数组形成的数字为110,然后可以被11整除。因此,所有元素都可以转换为零。

C++

// CPP implementation of the 
// above approach
#include 
using namespace std;
  
bool isPossibleToZero(int a[], int n)
{  
    // converting array element into number
    int num = 0;
    for (int i = 0; i < n; i++)
        num = num * 10 + a[i]; 
  
    // Check if divisible by 11
    return (num % 11 == 0);
}
  
// Driver program
int main()
{
    int arr[] = { 0, 1, 1, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    if (isPossibleToZero(arr, n))
        cout << "YES";
    else
        cout << "NO";
}

Java

// Java implementation of the above approach
import java.io.*;
  
class GFG {
      
    static boolean isPossibleToZero(int a[], int n)
    { 
        // converting array element into number
        int num = 0;
        for (int i = 0; i < n; i++)
            num = num * 10 + a[i]; 
      
        // Check if divisible by 11
        return (num % 11 == 0);
    }
      
    // Driver program
    public static void main (String[] args)
    {
  
        int arr[] = {0, 1, 1, 0};
        int n = arr.length;
        if (isPossibleToZero(arr, n))
                System.out.println( "YES");
        else
                System.out.println ("NO");
    }
}
  
// This code is contributed by @ajit

Python3

# Python3 implementation of the 
# above approach
  
def isPossibleToZero(a, n):
      
    # converting array element 
    # into number
    num = 0;
    for i in range(n):
        num = num * 10 + a[i]; 
  
    # Check if divisible by 11
    return (num % 11 == 0);
  
# Driver Code
arr = [ 0, 1, 1, 0 ];
n = len(arr);
if (isPossibleToZero(arr, n)):
    print("YES");
else:
    print("NO");
  
# This code is contributed mits

C#

// C# implementation of the above approach
using System;
  
class GFG 
{
      
static bool isPossibleToZero(int[] a, int n)
{ 
    // converting array element into number
    int num = 0;
    for (int i = 0; i < n; i++)
        num = num * 10 + a[i]; 
  
    // Check if divisible by 11
    return (num % 11 == 0);
}
  
// Driver Code
public static void Main()
{
    int[] arr = {0, 1, 1, 0};
    int n = arr.Length;
    if (isPossibleToZero(arr, n))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
  
// This code is contributed 
// by Akanksha Rai

的PHP


输出:
YES

上面的实现会导致稍大的数组溢出。我们可以使用下面的方法来避免溢出,检查是否可以将一个大数除以11