Chef and String | CHEFSTR1 | CodeChef
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 favorite 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 test cases. Then the test cases 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 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
2
6
1 6 11 6 10 11
4
1 3 5 7
Sample Output:
15
3
Explanation:
15
3
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
Solution (C++ 14):
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int num;
cin>>num;
int arr[num];
for(int i=0;i<num;i++)
{
cin>>arr[i];
}
long int sum=0;
for(int i=1; i<num;i++)
{
sum+=abs(arr[i]-arr[i-1])-1;
}
cout<<sum<<endl;
}
return 0;
}

Comments
Post a Comment