[Solution] Make It Increasing solution codeforces

Make It Increasing solution codeforces – Given 𝑛n integers 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an. You can perform the following operation on them:

  • select any element 𝑎𝑖ai (1𝑖𝑛1≤i≤n) and divide it by 22 (round down). In other words, you can replace any selected element 𝑎𝑖ai with the value 𝑎𝑖2⌊ai2⌋ (where 𝑥⌊x⌋ is – round down the real number 𝑥x).

[Solution] Make It Increasing solution codeforces

Output the minimum number of operations that must be done for a sequence of integers to become strictly increasing (that is, for the condition 𝑎1<𝑎2<<𝑎𝑛a1<a2<⋯<an to be satisfied). Or determine that it is impossible to obtain such a sequence. Note that elements of cannot be swapped. The only possible operation is described above.

For example, let 𝑛=3n=3 and a sequence of numbers [3,6,5][3,6,5] be given. Then it is enough to perform two operations on it:

  • Write the number 62=3⌊62⌋=3 instead of the number 𝑎2=6a2=6 and get the sequence [3,3,5][3,3,5];
  • Then replace 𝑎1=3a1=3 with 32=1⌊32⌋=1 and get the sequence [1,3,5][1,3,5].

The resulting sequence is strictly increasing because 1<3<51<3<5.

Input

The first line of the input contains an integer 𝑡t (1𝑡1041≤t≤104) — the number of test cases in the input.

The descriptions of the test cases follow.

The first line of each test case contains a single integer 𝑛n (1𝑛301≤n≤30).

The second line of each test case contains exactly 𝑛n integers 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an (0𝑎𝑖21090≤ai≤2⋅109).

[Solution] Make It Increasing solution codeforces

For each test case, print a single number on a separate line — the minimum number of operations to perform on the sequence to make it strictly increasing. If a strictly increasing sequence cannot be obtained, print “-1“.

Example
input

Copy
7
3
3 6 5
4
5 3 2 1
5
1 2 3 4 5
1
1000000000
4
2 8 7 5
5
8 26 5 21 10
2
5 14
output

Copy
2
-1
0
0
4
11
0

[Solution] Make It Increasing solution codeforces

The first test case is analyzed in the statement.

In the second test case, it is impossible to obtain a strictly increasing sequence.

In the third test case, the sequence is already strictly increasing.

For Solution

Click here

Leave a Comment