Even Array
Practice
2.9 (10 votes)
Introduction to dynamic programming 1
Algorithms
Dynamic programming
Bit manipulation
Problem
40% Success 1584 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code
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 \(A_i =\lfloor \frac{A_i}{2} \rfloor\)
- Choose an index \(1 \le i \lt \mid A\mid\)and replace \(A_i, A_{i+1}\) with one occurrence of \(A_i \oplus A_{i+1}\). Here \(\mid A \mid\) denotes the length of the array A and \(\oplus\) denotes bitwise XOR operation.
Find the minimum number of moves needed to make array A good.
Input format
- The first line contains T denoting the number of test cases. The description of T test cases is as follows:
- For each test case:
- The first line contains N denoting the size of array A.
- The second line contains N space-separated integers \(A_1, A_2, \dots, A_N\) - denoting the elements of A.
Output format
For each test case, print the minimum number of moves needed to make array A good.
Constraints
\(1 \leq T \leq 10^4\)
\(2 \leq N \leq 10^5\)
\(1\leq A_i \lt 2 ^{30}\)
\(Sum\; of \; N\;over\;all\;test\;cases\;does\;not\;exceed\;2\cdot 10^5.\)
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] \(\oplus\) A[2] = 1 \(\oplus\) 3 = 2, making A = [2, 8]. Now the array contains all even numbers.
Code Editor
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Submissions
Please login to view your submissions
Similar Problems
Points:50
5 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingGrammar-VerifiedHardHiringRecruit
Points:50
3 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingHardReadyRecruitTwo pointer
Points:50
1 votes
Tags:
Dynamic ProgrammingIntroduction to Dynamic Programming 1Algorithms
Editorial
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor