[Solution] Make A-B Same Solution Codechef

Make A-B Same Solution Codechef

You are given two binary arrays  and , each of size  (�≥3).

You can perform the following operation on array :

  • Select three indexes �,�, and  (1≤�<�<�≤�),
    and set ��=��  ��  ��. Here  denotes the bitwise OR operation.

Determine whether it is possible to convert the array  to array  by applying any (possibly zero) number of given operations.

Note that, in a binary array each element is either 0 or 1.

Input Format Make A-B Same Solution Codechef

  • The first line of input will contain    a single integer , denoting the number of test cases.
  • Each test case consists of multiple lines of input:
    • The first line of each test case contains a single integer  — size of arrays.
    • The second line of each test case contains  space-separated integers �1,�2,�3,…,��, the elements of array .
    • The third line of each test case contains  space-separated integers �1,�2,�3,…,��, the elements of array .

Output Format Make A-B Same Solution Codechef

For each test case, output YES, it is possible to convert the array  to array  using any number of given operations. Otherwise, output NO.

You can output each character of the answer in uppercase or lowercase. For example, the strings yEsyesYes, and YES are considered the same.

Constraints Make A-B Same Solution Codechef

  • 1≤�≤105
  • 3≤�≤105
  • �� and �� are either 0 or 1.
  • The sum of  over all test cases won’t exceed 5⋅105.

Sample 1: Make A-B Same Solution Codechef

Input

Output

3
5
0 1 0 0 1
0 1 1 1 1
3
1 0 0
0 0 1
3
0 0 0
0 1 0
YES
NO
NO

Explanation: Make A-B Same Solution Codechef

Test case 1: We can perform the following operations to make the arrays equal:

  • Operation 1: Select �=2,�=3,�=4 and set �3=�2  �3  �4 =1  0  0=1. Thus, the array becomes �=[0,1,1,0,1].
  • Operation 2: Select �=3,�=4,�=5 and set �4=�3  �4  �5 =1  0  1=1. Thus, the array becomes �=[0,1,1,1,1].

The array  is thus converted to array .

Test case 2: It is not possible to convert the array  to array  using any number of given operation.

Test case 3: It is not possible to convert the array  to array  using any number of given operation.

Leave a Comment