Chef and Strings|| codechef ||beginner solution||programming info

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.
fig1
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 i

Subtasks

  • 30 points : for each valid i
  • 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 1
  • Chef skips  strings (2,3,4,5) to move from  to 
  • Chef skips  strings (7,8,9,10) to move from  to 
  • Chef skips  strings (10,9,8,7) to move from  to 
  • Chef skips  strings (7,8,9) to move from 6 to 
  • Chef skips  strings to move from  to 
Therefore, the answer is 4+4+4+3+0=15
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 using c++ language:-
#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;

}

Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support