Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

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 $1^{st}$ string and then the $6^{th}$ string. This is easy in guitars with only $6$ strings; However, Chef is playing a guitar with $10^{6}$ 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.

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 - $S_{1}$, $S_{2}$, ..., $S_{N}$, where $S_{i}$ is the number of the $i^{th}$ 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. 

------ Constraints  ------
$1 ≤ T ≤ 10$
$2 ≤ N ≤ 10^{5}$
$1 ≤ S_{i} ≤ 10^{6}$
For each valid $i$, $S_{i} \neq S_{i+1}$

------ Subtasks ------
30 points : for each valid $i$, $S_{i} < S_{i+1}$
70 points : No additional constraints

----- Sample Input 1 ------ 
2

6

1 6 11 6 10 11

4

1 3 5 7
----- Sample Output 1 ------ 
15

3
----- explanation 1 ------ 
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$

Source: codechef | Difficulty: EASY
Tags: Implementation


Solutions:
t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	count = 0
	for i in range(n - 1):
		count += max(arr[i], arr[i + 1]) - min(arr[i], arr[i + 1]) - 1
	print(count)
	t -= 1


cases = int(input())
for _ in range(cases):
	n = int(input())
	s = list(map(int, input().split()))
	x = 0
	for i in range(1, n):
		x = x + abs(s[i] - s[i - 1]) - 1
	print(x)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	for i in range(1, n):
		s += abs(l[i] - l[i - 1]) - 1
	print(s)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	tot = 0
	for i in range(1, n):
		tot += abs(s[i] - s[i - 1]) - 1
	print(tot)


for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	total_sum = 0
	for i in range(N - 1):
		total_sum += abs(a[i + 1] - a[i]) - 1
	print(total_sum)


for i in range(int(input())):
	N = int(input())
	li = list(map(int, input().split()))
	diff = 0
	for i in range(1, N):
		diff += abs(li[i] - li[i - 1]) - 1
	print(diff)


for tc in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	for i in range(n - 1):
		a = abs(l[i] - l[i + 1]) - 1
		sum = sum + a
	print(sum)


T = int(input())
for i in range(T):
	n = int(input())
	s = list(map(int, input().split()))
	c = 0
	for i in range(1, n):
		c += abs(s[i] - s[i - 1]) - 1
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	for i in range(1, n):
		c += abs(l[i] - l[i - 1]) - 1
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	p = 0
	s = list(map(int, input().split()))
	for i in s:
		a.append(int(i))
	for i in range(0, len(a) - 1):
		k = a[i + 1] - a[i]
		if k < 0:
			k = k + 1
		else:
			k = k - 1
		p += abs(k)
	print(p)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	sum = 0
	for j in range(0, n - 1):
		sum = sum + abs(s[j] - s[j + 1]) - 1
	print(sum)


T = int(input())
for t in range(0, T):
	N = int(input())
	S = list(map(int, input().split()))
	sum_skips = 0
	for i in range(0, N - 1):
		sum_skips += abs(S[i + 1] - S[i]) - 1
	print(sum_skips)


for i in range(int(input())):
	n = int(input())
	sum1 = 0
	lst = list(map(int, input().split()))
	for i in range(n - 1):
		if lst[i + 1] > lst[i]:
			diff = lst[i + 1] - lst[i] - 1
			sum1 = sum1 + diff
		elif lst[i] > lst[i + 1]:
			diff = lst[i] - lst[i + 1] - 1
			sum1 = sum1 + diff
	print(sum1)


for p in range(int(input())):
	b = int(input())
	a = list(map(int, input().split()))
	count = 0
	for i in range(1, b):
		count += abs(a[i] - a[i - 1]) - 1
	print(count)


for j in range(int(input())):
	x = int(input())
	l = list(map(int, input().split()))
	s = 0
	for i in range(1, x):
		s += abs(l[i] - l[i - 1]) - 1
	print(s)


for i in range(int(input())):
	x = int(input())
	v = list(map(int, input().split()))
	c = 0
	for i in range(len(v) - 1):
		b = v[i] - v[i + 1]
		if b < 0:
			b = b * -1
		b = b - 1
		c = c + b
	print(c)


for i in range(int(input())):
	n = int(input())
	sum = 0
	l = list(map(int, input().split()))
	for i in range(n - 1):
		m = abs(l[i] - l[i + 1]) - 1
		sum = sum + m
	print(sum)


for _po in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	count = 0
	for i in range(1, n):
		count += abs(a[i] - a[i - 1]) - 1
	print(count)


import math
for t in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	c = 0
	for i in range(1, n):
		c += abs(li[i] - li[i - 1]) - 1
	print(c)


for _ in range(int(input())):
	N = int(input())
	L = list(map(int, input().split()))
	D = 0
	for i in range(N - 1):
		D += abs(L[i] - L[i + 1]) - 1
	print(D)


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	t = 0
	for i in range(1, len(s)):
		t = t + abs(s[i] - s[i - 1]) - 1
	print(t)


for i in range(int(input())):
	N = int(input())
	S = list(map(int, input().split()))
	c = 0
	for j in range(N - 1):
		c = c + (abs(S[j] - S[j + 1]) - 1)
	print(c)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	skip = 0
	for i in range(n - 1):
		s = arr[i]
		e = arr[i + 1]
		if s > e:
			(s, e) = (e, s)
		skip += e - s - 1
	print(skip)


n = int(input())
for _ in range(n):
	arr = []
	arr1 = []
	a = int(input())
	arr = list(map(int, input().split()))
	i = 0
	j = 0
	for i in range(a - 1):
		j = i + 1
		if arr[i] > arr[j]:
			res = arr[i] - arr[j] - 1
		if arr[j] > arr[i]:
			res = arr[j] - arr[i] - 1
		arr1.append(res)
	print(sum(arr1))


t = int(input())
while t != 0:
	t = t - 1
	n = int(input())
	a = [int(i) for i in input().split()]
	count = 0
	ans = 0
	for i in range(n - 1):
		x = abs(a[i] - a[i + 1])
		x = x - 1
		ans += x
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	strings = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		ans += abs(strings[i] - strings[i - 1]) - 1
	print(ans)


for t in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	b = 0
	for i in range(n - 1):
		a = abs(s[i] - s[i + 1]) - 1
		b = b + a
	print(b)


q = int(input())
for i in range(q):
	n = int(input())
	s = list(map(int, input().split()))
	count = 0
	for j in range(n - 1):
		count += abs(s[j + 1] - s[j]) - 1
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	strings = input().split(' ')
	plucks = 0
	for j in range(n - 1):
		plucks += abs(int(strings[j]) - int(strings[j + 1])) - 1
	print(plucks)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	l = []
	for k in range(len(arr) - 1):
		z = abs(arr[k] - arr[k + 1]) - 1
		l.append(z)
	print(sum(l))


t = int(input())
for tc in range(t):
	n = int(input())
	d = 0
	b = []
	a = list(map(int, input().split()))
	for j in range(1, n):
		d = abs(a[j] - a[j - 1]) - 1
		b.append(d)
	print(sum(b))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	sm = 0
	for j in range(1, n):
		sm = sm + abs(a[j] - a[j - 1]) - 1
	print(sm)


q = int(input())
for i in range(q):
	w = int(input())
	e = str(input())
	e = e + ' '
	r = ''
	t = []
	for i in e:
		if i == ' ':
			t.append(int(r))
			r = ''
		else:
			r = r + i
	u = 0
	for i in range(1, w):
		if t[i] >= t[i - 1]:
			u = u + (t[i] - t[i - 1] - 1)
		if t[i] < t[i - 1]:
			u = u + (t[i - 1] - t[i] - 1)
	print(u)


for j in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = 0
	for i in range(a - 1):
		c += abs(b[i] - b[i + 1]) - 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	res = 0
	for p in range(n - 1):
		res += abs(s[p + 1] - s[p]) - 1
	else:
		print(res)


t = int(input())
for i in range(t):
	s = int(input())
	z = 0
	l = list(map(int, input().split()))
	for i in range(s - 1):
		ma = max(l[i + 1], l[i])
		mi = min(l[i + 1], l[i])
		c = ma - mi
		c = c - 1
		z = z + c
	print(z)


t = int(input())
for i in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	skips = 0
	for i in range(n - 1):
		if abs(s[i] - s[i + 1]) == 1:
			continue
		else:
			skips += abs(s[i] - s[i + 1]) - 1
	print(skips)


for _ in range(int(input())):
	plucks = int(input())
	sum = 0
	liststrings = list(map(int, input().split()))
	for i in range(plucks - 1):
		sum = sum + (abs(liststrings[i + 1] - liststrings[i]) - 1)
	print(sum)


q = int(input())
for i in range(q):
	p = int(input())
	r = list(map(int, input().split()))
	c = 0
	for j in range(p - 1):
		c = c + abs(r[j] - r[j + 1]) - 1
	print(c)


t = int(input())
for i in range(t):
	p = int(input())
	z = list(map(int, input().split()))
	c = 0
	for j in range(p - 1):
		c = c + abs(z[j] - z[j + 1]) - 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = []
	for i in range(n - 1, 0, -1):
		x = a[i] - a[i - 1]
		c.append(x)
	d = []
	for i in c:
		d.append(abs(i))
	print(sum(d) - n + 1)


for _ in range(int(input())):
	l = int(input())
	a = [int(x) for x in input().split()]
	ans = 0
	for i in range(1, l):
		ans += abs(a[i] - a[i - 1]) - 1
	print(ans)


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	diff = 0
	ans = 0
	for k in range(n - 1):
		diff = abs(s[k] - s[k + 1]) - 1
		ans += diff
	print(ans)


T = int(input())
while T > 0:
	N = int(input())
	S = list(map(int, input().split()))
	total = 0
	for i in range(N):
		if i + 1 < N:
			total += abs(S[i] - S[i + 1]) - 1
	print(total)
	T -= 1


def difference(string1, string2):
	M = 0
	for j in range(string1 - 1):
		differ = abs(string2[j] - string2[j + 1])
		M += differ - 1
	return M
for number in range(int(input())):
	string1 = int(input())
	string2 = list(map(int, input().split()))
	diff = difference(string1, string2)
	print(diff)


t = int(input())
for i in range(t):
	a = int(input())
	num = list(map(int, input().split()))
	count = 0
	for i in range(1, a):
		count += max(num[i], num[i - 1]) - min(num[i], num[i - 1]) - 1
	print(count)


T = int(input())
for i in range(T):
	n = int(input())
	inp = input()
	list1 = inp.split()
	list_int = []
	for i in list1:
		j = int(i)
		list_int.append(j)
	list_diff = []
	for i in range(0, len(list_int) - 1):
		diff = abs(list_int[i + 1] - list_int[i])
		list_diff.append(diff - 1)
	print(sum(list_diff))


for _ in range(int(input())):
	t = int(input())
	l1 = list(map(int, input().split()))
	count = 0
	for i in range(1, t):
		count += abs(l1[i] - l1[i - 1]) - 1
	print(count)


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	f = 0
	for i in range(a - 1):
		c = b[i]
		d = b[i + 1]
		e = abs(c - d) - 1
		f = f + e
	print(f)


for _ in range(int(input())):
	N = int(input())
	S = list(map(int, input().split()))
	s = 0
	for i in range(N - 1):
		if S[i] != S[i + 1]:
			s += abs(S[i] - S[i + 1]) - 1
	print(s)