Even Array

2.9

10 votes
Introduction to Dynamic Programming 1, Algorithms, Dynamic Programming, Bit manipulation
Problem

An array is called good if every element of the array is an even integer.
You are given an array A of length N. In one move, you can do one of the following operations:

  • Choose an index i and set Ai=Ai2
  • Choose an index 1i<∣Aand replace Ai,Ai+1 with one occurrence of AiAi+1. Here A denotes the length of the array A and  denotes bitwise XOR operation.

Find the minimum number of moves needed to make array A good.

 Input format

  • The first line contains denoting the number of test cases. The description of T test cases is as follows:
  • For each test case:
    • The first line contains denoting the size of array A.
    • The second line contains N space-separated integers A1,A2,,AN - denoting the elements of A.

Output format
For each test case, print the minimum number of moves needed to make array A good.

Constraints

1T1042N1051Ai<230SumofNoveralltestcasesdoesnotexceed2105.

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Test Case 1: One possible sequence of moves is:

  • Choose i = 1, set A[1] = 11 / 2 = 5, making A = [5, 2]
  • Choose i = 1, set A[1] = 5 / 2 = 2, making A = [2, 2]. Now the array contains all even numbers.

Test Case 2: 

  • Choose i = 1, replace A[1], A[2] with A[1]  A[2] = 1  3 = 2, making A = [2, 8]. Now the array contains all even numbers.
Editor Image

?