-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChefandStrings.py
51 lines (43 loc) · 1.89 KB
/
ChefandStrings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Having already mastered cooking, Chef has now decided to learn how to play the guitar. Often while trying to play a song, Chef has to skip several strings to reach the string he has to pluck. Eg. he may have to pluck the 1st string and then the 6th string. This is easy in guitars with only 6 strings; However, Chef is playing a guitar with 106 strings. In order to simplify his task, Chef wants you to write a program that will tell him the total number of strings he has to skip while playing his favourite song.
fig1
This is how guitar strings are numbered (In ascending order from right to left). Eg. to switch from string 1 to 6, Chef would have to skip 4 strings (2,3,4,5).
Input:
First line will contain T, number of testcases. Then the testcases follow.
The first line of each test case contains N, the number of times Chef has to pluck a string
The second line of each test case contains N space separated integers - S1, S2, …, SN, where Si is the number of the ith string Chef has to pluck.
Output:
For each testcase, output the total number of strings Chef has to skip over while playing his favourite song.
Sample Input:
2
6
1 6 11 6 10 11
4
1 3 5 7
Sample Output:
15
3
Explanation:
Test Case 1
Chef skips 4 strings (2,3,4,5) to move from 1 to 6
Chef skips 4 strings (7,8,9,10) to move from 6 to 11
Chef skips 4 strings (10,9,8,7) to move from 11 to 6
Chef skips 3 strings (7,8,9) to move from 6 to 10
Chef skips 0 strings to move from 10 to 11
Therefore, the answer is 4+4+4+3+0=15
Test Case 2
Chef skips 1 string to move from 1 to 3
Chef skips 1 string to move from 3 to 5
Chef skips 1 string to move from 5 to 7
Therefore, the answer is 1+1+1=3
"""
# SOLUTION
test_cases = int(input())
while test_cases > 0 :
N = int(input())
Strings = list(map(int, input().split()))
count = 0
for i in range(0,N-1) :
count += abs(Strings[i] - Strings[i+1]) - 1
print(count)
test_cases -= 1