Chef and Strings
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 string and then the string. This is easy in guitars with only strings; However, Chef is playing a guitar with 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 to , Chef would have to skip strings .
Input:
- First line will contain , number of testcases. Then the testcases follow.
- The first line of each test case contains , the number of times Chef has to pluck a string
- The second line of each test case contains space separated integers - , …, , where is the number of the 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
- For each valid ,
Subtasks
- 30 points : for each valid ,
- 70 points : No additional constraints
Sample Input:
2
6
1 6 11 6 10 11
4
1 3 5 7
Sample Output:
15
3
Explanation:
Test Case
- Chef skips strings to move from to
- Chef skips strings to move from to
- Chef skips strings to move from to
- Chef skips strings to move from to
- Chef skips strings to move from to
Therefore, the answer is
Test Case
- Chef skips string to move from to
- Chef skips string to move from to
- Chef skips string to move from to
Therefore, the answer is
#include <iostream>
#include<cmath>
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
ll previous,next,diff,sum=0;
ll s[n];
cin>>previous;
for(int i=1;i<n;i++)
{
cin>>next;
diff=abs(previous-next);
previous=next;
if(diff>1)
{
diff-=1;
sum+=diff;
}
}
cout<<sum<<"\n";
}
// your code goes here
return 0;
}
No comments:
Post a Comment