-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.cs
109 lines (99 loc) · 3.96 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Problem: https://www.hackerrank.com/challenges/cats-and-a-mouse/problem
C# Language Version: 6.0
.Net Framework Version: 4.5.2
Thoughts :
1. Get the next set of location for cat A, cat B and the mouse
2. In the current set, let location of cat A be catALocation, location of cat B be catBLocation, location of mouse be mouseLocation
3. Now we simply need to compare the proximity of mouse with cat A and cat B.
There are below possibilities of locations of cat A, cat B and mouseLocation on x-axis
- cat A cat B Mouse
- cat A Mouse cat B
- mouse Cat A Cat B
- cat B cat A Mouse
- cat B Mouse cat A
- mouse Cat B Cat A
- Cat A and Cat B at exactly same location
4. If cat A is found to be nearer to mouse then print "Cat A"
5. If cat B is found to be nearer to mouse then print "Cat B"
6. If both cats are equidistant from mouse then print "Mouse C"
7. Repeat step 1 to 6 for all set of input locations.
Time Complexity: O(n)
Space Complexity: O(1)
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args)
{
var output = "Mouse C";
var q = int.Parse(Console.ReadLine());
for (int a0 = 0; a0 < q; a0++)
{
var tokens_x = Console.ReadLine().Split(' ');
var catALocation = int.Parse(tokens_x[0]);
var catBLocation = int.Parse(tokens_x[1]);
var mouseLocation = int.Parse(tokens_x[2]);
if (catALocation < catBLocation)
{
if (catBLocation <= mouseLocation)
{
//cat A cat B Mouse
output = "Cat B";
}
else
{
if (mouseLocation > catALocation)
{
//cat A Mouse cat B
if (mouseLocation - catALocation > catBLocation - mouseLocation)
output = "Cat B";
else if (mouseLocation - catALocation < catBLocation - mouseLocation)
output = "Cat A";
else
output = "Mouse C";
}
else
{
//mouse Cat A Cat B
output = "Cat A";
}
}
}
else if (catALocation > catBLocation)
{
if (catALocation <= mouseLocation)
{
//cat B cat A Mouse
output = "Cat A";
}
else
{
if (mouseLocation > catBLocation)
{
//cat B Mouse cat A
if (catALocation - mouseLocation > mouseLocation - catBLocation)
output = "Cat B";
else if (catALocation - mouseLocation < mouseLocation - catBLocation)
output = "Cat A";
else
output = "Mouse C";
}
else
{
//mouse Cat B Cat A
output = "Cat B";
}
}
}
else
{
//cat fight will let the mouse run-away.
output = "Mouse C";
}
Console.WriteLine(output);
}
}
}