2020 Updated Cats and a Mouse Hackerrank solution by Coding Demons


    

          Coding Demons has solved Cats and a Mouse a problem on Hackerrank. The Solution is given in two stages.

Stage 1 Logic

Stage 2 Code

                     


For better understanding refer our YouTube Tutorial Video.


LOGIC 

  • Take no. of test cases.
  • For each test case take locations of Cat A, Cat B and Mouse C.
  • Calculate absolute distance from Mouse C to both the Cats.
  • If Cat A to Mouse C distance is minimum print Cat A.
  • Else If Cat B to Mouse C distance is minimum print Cat B.
  • Else print Mouse C as distance will be equal.


CODE

/*** SOLUTION BY CODING DEMONS ***/

#include <bits/stdc++.h>
using namespace std;

int main () {

int n;    //no of test cases(queries)
cin>>n;


for (int i=0;i<n;i++){

//take locations of Cat A, Cat B & Mouse C
    int a,b,c;
    cin>>a>>b>>c;   


int x=abs(a-c);
//distance of mouse from Cat A

  int y=abs(b-c);
//distance of mouse from Cat B

    if (x<y)  cout<<"Cat A"<<endl;          
       //Mouse near to Cat A

    else if (y<x)  cout<<"Cat B"<<endl;             
     //Mouse near to Cat B

    else  cout<<"Mouse C"<<endl; 
//Mouse at equal distance from both the cats

    }
    return 0;
}

/*** END ***/






!! SUBSCRIBE FOR MORE CODES & VIDEOS !!


Post a Comment

0 Comments