Sunday, May 15, 2022

Equal After And

 You are given an array 

A=[A1,A2,,AN], consisting of N integers. In one move, you can take two adjacent numbers Ai and Ai+1, delete them, and then insert the number AiAi+1 at the deleted position. Here,  denotes bitwise AND. Note that after this operation, the length of the array decreases by one.

Formally, as long as |A|>1 (where |A| denotes the current length of A), you can pick an index 1i<|A| and transform A into [A1,A2,,Ai1,AiAi+1,Ai+2,,A|A|].

Find the minimum number of moves required to make all numbers in the resulting array equal.

Input Format

  • The first line of input contains an integer T — the number of test cases you need to solve.
  • The first line of each test case contains one integer N, the size of the array.
  • The second line of each test case contains N space-separated integers A1,,AN — the elements of the array A.

Output Format

For each test case, output on a new line the minimum number of moves required to make all numbers equal.

Constraints

  • 1T106
  • 2N106
  • Sum of N over all test cases is at most 106.
  • 0Ai<230

Subtasks

  • Subtask 1 (20 points):
    • 0Ai255
    • Sum of N over all test cases is at most 255.
  • Subtask 2 (30 points):
    • Sum of N over all test cases is at most 2000.
  • Subtask 3 (50 points):
    • Original constraints.

Sample Input 1 

4
4
0 0 0 1
2
1 1
6
1 2 3 4 5 6
4
2 28 3 22

Sample Output 1 

1
0
4
3

Explanation

Test case 1: Choose i=3 to make the array [0,0,01]=[0,0,0].

Test case 2: All elements of the array are already equal.

Test case 3: One possible sequence of moves is as follows:

  • Choose i=1, making the array [12,3,4,5,6]=[0,3,4,5,6]
  • Choose i=2, making the array [0,0,5,6]
  • Choose i=3, making the array [0,0,4]
  • Choose i=2, making the array [0,0]

It can be verified that in this case, making every element equal using 3 or fewer moves is impossible.

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...