LIFT REQUESTS - LIFTME CodeCHEF Easy Question

Welcome back to my blog .

Here is a link to the PROBLEM

We just need to find the number of floors the lift travelled . So just compute modulus of b-a for all queries and add them to the sum .

Just take a look at the following C++ code :-

#include <iostream>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        long long int q;
        cin>>n;
        cin>>q;
        long int i=0;
        unsigned long long int ans=0;
        long long int f,d,prev=0;
        
        while(i!=q)
        {
            cin>>f>>d;
            ans=ans+abs(f-prev);
            ans=ans+abs(d-f);
            prev=d;
            i++;
        }
        cout<<ans<<endl;
    }
// your code goes here
return 0;
}


Please understand this and leave your doubts in the comments section .

Comments

Popular Posts