You are given two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ elements, each element is either $$$0$$$ or $$$1$$$.
You can make operations of $$$2$$$ kinds.
Find the minimum number of operations required to make $$$a$$$ equal to $$$b$$$.
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the length of the arrays $$$a$$$ and $$$b$$$.
The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,\ldots,a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), representing the array $$$a$$$.
The third line of each test case contains $$$n$$$ space-separated integers $$$b_1,b_2,\ldots,b_n$$$ ($$$b_i$$$ is $$$0$$$ or $$$1$$$), representing the array $$$b$$$.
For each test case, print the minimum number of operations required to make $$$a$$$ equal to $$$b$$$.
531 0 10 0 141 1 0 00 1 1 121 11 141 0 0 10 1 1 0101
1 2 0 1 1
In the first case, we need only one operation: change $$$a_1$$$ to $$$1-a_i$$$. Now $$$a = [0, 0]$$$ which is equal to $$$b$$$.
In the second case, the optimal way is to rearrange $$$a$$$ to get the array $$$[0, 1, 11$$$. Now $$$a = [0, 0, 1]$$$ which is equal to $$$b$$$.
In the second case, one of optimal ways would be to first change $$$a_3$$$ to $$$1 - a_3$$$, then rearrange $$$a$$$.
In the third case, no operation is needed.
In the fourth case, the optimal way is to rearrange $$$a$$$ to get the array $$$[0, 1, 1, 0]$$$.
You are given an integer array $$$a$$$ of length $$$n$$$.
Does there exist an array $$$b$$$ consisting of $$$n+1$$$ positive integers such that $$$a_i=\gcd (b_i,b_{i+1})$$$ for all $$$i$$$ ($$$1 \leq i \leq n$$$)?
Note that $$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^5$$$). Description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,\ldots,a_n$$$ representing the array $$$a$$$ ($$$1 \leq a_i \leq 10^4$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output "YES" if such $$$b$$$ exists, otherwise output "NO". You can print each letter in any case (upper or lower).
4134324 234 2 441 1 1 1
YES YES NO YES
In the first test case, we can take $$$b=[343,343]$$$.
In the second test case, one possibility for $$$b$$$ is $$$b=[12,8,6]$$$.
In the third test case, it can be proved that there does not exist any array $$$b$$$ that fulfills all the conditions.
This is the easy version of this problem. In this version, we do not have queries. Note that we have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.
An array $$$b$$$ of length $$$m$$$ is good if for all $$$i$$$ the $$$i$$$-th element is greater than or equal to $$$i$$$. In other words, $$$b$$$ is good if and only if $$$b_i \geq i$$$ for all $$$i$$$ ($$$1 \leq i \leq m$$$).
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find the number of pairs of indices $$$(l, r)$$$, where $$$1 \le l \le r \le n$$$, such that the array $$$[a_l, a_{l+1}, \ldots, a_r]$$$ is good.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^5$$$). Description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$), the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq n$$$), representing the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print the number of suitable pairs of indices.
331 2 331 1 142 1 4 3
6 3 7
In the first test case, all subarrays of $$$a$$$ are good, so all pairs are suitable.
In the second test case, the pairs $$$(1, 1)$$$, $$$(2, 2)$$$, and $$$(3, 3)$$$ are suitable. For example, when $$$(l, r) = (1, 2)$$$, the array $$$b=[1,1]$$$ is not good because $$$b_2 \lt 2$$$.
This is the hard version of this problem. In this version, we have queries. Note that we do not have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.
An array $$$b$$$ of length $$$m$$$ is good if for all $$$i$$$ the $$$i$$$-th element is greater than or equal to $$$i$$$. In other words, $$$b$$$ is good if and only if $$$b_i \geq i$$$ for all $$$i$$$ ($$$1 \leq i \leq m$$$).
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, and you are asked $$$q$$$ queries.
In each query, you are given two integers $$$p$$$ and $$$x$$$ ($$$1 \leq p,x \leq n$$$). You have to do $$$a_p := x$$$ (assign $$$x$$$ to $$$a_p$$$). In the updated array, find the number of pairs of indices $$$(l, r)$$$, where $$$1 \le l \le r \le n$$$, such that the array $$$[a_l, a_{l+1}, \ldots, a_r]$$$ is good.
Note that all queries are independent, which means after each query, the initial array $$$a$$$ is restored.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$).
The third line contains an integer $$$q$$$ ($$$1 \leq q \leq 2 \cdot 10^5$$$) — the number of queries.
Each of the next $$$q$$$ lines contains two integers $$$p_j$$$ and $$$x_j$$$ ($$$1 \leq p_j, x_j \leq n$$$) – the description of the $$$j$$$-th query.
For each query, print the number of suitable pairs of indices after making the change.
4 2 4 1 4 3 2 4 3 3 2 1
6 10 5
5 1 1 3 2 1 3 1 3 2 5 4 5
7 9 8
Here are notes for first example.
In first query, after update $$$a=[2,4,1,4]$$$. Now $$$(1,1)$$$, $$$(2,2)$$$, $$$(3,3)$$$, $$$(4,4)$$$, $$$(1,2)$$$, and $$$(3,4)$$$ are suitable pairs.
In second query, after update $$$a=[2,4,3,4]$$$. Now all subarrays of $$$a$$$ are good.
In third query, after update $$$a=[2,1,1,4]$$$. Now $$$(1,1)$$$, $$$(2,2)$$$, $$$(3,3)$$$, $$$(4,4)$$$, and $$$(3,4)$$$ are suitable.
Everool has a binary string $$$s$$$ of length $$$2n$$$. Note that a binary string is a string consisting of only characters $$$0$$$ and $$$1$$$. He wants to partition $$$s$$$ into two disjoint equal subsequences. He needs your help to do it.
You are allowed to do the following operation exactly once.
In other words, you can select a sequence of indices $$$b_1, b_2, \ldots, b_m$$$, where $$$1 \le b_1 \lt b_2 \lt \ldots \lt b_m \le 2n$$$. After that you simultaneously set $$$$$$s_{b_1} := s_{b_m},$$$$$$ $$$$$$s_{b_2} := s_{b_1},$$$$$$ $$$$$$\ldots,$$$$$$ $$$$$$s_{b_m} := s_{b_{m-1}}.$$$$$$
Can you partition $$$s$$$ into two disjoint equal subsequences after performing the allowed operation exactly once?
A partition of $$$s$$$ into two disjoint equal subsequences $$$s^p$$$ and $$$s^q$$$ is two increasing arrays of indices $$$p_1, p_2, \ldots, p_n$$$ and $$$q_1, q_2, \ldots, q_n$$$, such that each integer from $$$1$$$ to $$$2n$$$ is encountered in either $$$p$$$ or $$$q$$$ exactly once, $$$s^p = s_{p_1} s_{p_2} \ldots s_{p_n}$$$, $$$s^q = s_{q_1} s_{q_2} \ldots s_{q_n}$$$, and $$$s^p = s^q$$$.
If it is not possible to partition after performing any kind of operation, report $$$-1$$$.
If it is possible to do the operation and partition $$$s$$$ into two disjoint subsequences $$$s^p$$$ and $$$s^q$$$, such that $$$s^p = s^q$$$, print elements of $$$b$$$ and indices of $$$s^p$$$, i. e. the values $$$p_1, p_2, \ldots, p_n$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). Description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), where $$$2n$$$ is the length of the binary string.
The second line of each test case contains the binary string $$$s$$$ of length $$$2n$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, follow the following output format.
If there is no solution, print $$$-1$$$.
Otherwise,
If there are multiple solutions, print any.
4 2 1010 3 100010 2 1111 2 1110
0 1 2 2 3 5 1 2 5 3 2 3 4 1 4 -1
In the first test case, $$$b$$$ is empty. So string $$$s$$$ is not changed. Now $$$s^p = s_1 s_2 = \mathtt{10}$$$, and $$$s^q = s_3s_4 = \mathtt{10}$$$.
In the second test case, $$$b=[3,5]$$$. Initially $$$s_3=\mathtt{0}$$$, and $$$s_5=\mathtt{1}$$$. On performing the operation, we simultaneously set $$$s_3=\mathtt{1}$$$, and $$$s_5=\mathtt{0}$$$.
So $$$s$$$ is updated to 101000 on performing the operation.
Now if we take characters at indices $$$[1,2,5]$$$ in $$$s^p$$$, we get $$$s_1=\mathtt{100}$$$. Also characters at indices $$$[3,4,6]$$$ are in $$$s^q$$$. Thus $$$s^q=100$$$. We are done as $$$s^p=s^q$$$.
In fourth test case, it can be proved that it is not possible to partition the string after performing any operation.
You're given an array consisting of $$$n$$$ integers. You have to perform $$$n$$$ turns.
Initially your score is $$$0$$$.
On the $$$i$$$-th turn, you are allowed to leave the array as it is or swap any one pair of $$$2$$$ adjacent elements in the array and change exactly one of them to $$$0$$$(and leave the value of other element unchanged) after swapping. In either case(whether you swap or not), after this you add $$$a_i$$$ to your score.
What's the maximum possible score you can get?
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 500$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
Print a single integer — the maximum possible score.
2 3 1
6
5 7 3 9 6 12
52
In the first example, to get the maximum score we do as follows. Do nothing on the first turn, add $$$3$$$ to the score. Swap the first and the second elements and turn $$$1$$$ to $$$0$$$ on the second turn, and add $$$3$$$ to the score. The final score is $$$6$$$.