To solve this problem, we need to analyze the parity and the sum of the coordinates.
-
Long Steps: Each long step increases either
$x$ or$y$ by 2. This means long steps do not change whether a coordinate is even or odd. -
Short Step: Yousef can take at most one short step. A short step increases either
$x$ or$y$ by 1. -
Reachability:
- If Yousef takes zero short steps: He can reach
$(x, y)$ if and only if both$x$ and$y$ are even and non-negative. - If Yousef takes one short step: He can reach
$(x, y)$ if exactly one of the coordinates is odd and the other is even (and both are non-negative).
- If Yousef takes zero short steps: He can reach
-
Simplified Rule:
- If
$x < 0$ or$y < 0$ , he cannot reach it. - Calculate the total "units" of distance:
$S = x + y$ . - If both
$x$ and$y$ are even, he reaches it using only long steps (Sum$x+y$ is even). - If one is odd and one is even, he reaches it using exactly one short step (Sum
$x+y$ is odd). -
If both
$x$ and$y$ are odd, he would need at least two short steps (one for$x$ and one for$y$ ), which is forbidden.
- If
Conclusion: Yousef can reach
#include <iostream>
using namespace std;
/**
* Problem Analysis:
* Long step: +2 to x OR +2 to y
* Short step: +1 to x OR +1 to y (Max 1 allowed)
*
* If x is odd and y is odd:
* To make x odd, we need at least one short step in x direction.
* To make y odd, we need at least one short step in y direction.
* Total short steps needed = 2. This is not allowed.
*
* Otherwise, if at least one is even, we can reach it using 0 or 1 short steps.
*/
bool canReachKoshary(long long x, long long y) {
// Cannot move in negative directions
if (x < 0 || y < 0) {
return false;
}
// Check if both are odd
if (x % 2 != 0 && y % 2 != 0) {
return false; // Needs 2 short steps
}
return true; // Either both even (0 short steps) or one even/one odd (1 short step)
}
int main() {
long long x, y;
cout << "Enter target coordinates (x y): ";
if (!(cin >> x >> y)) return 0;
if (canReachKoshary(x, y)) {
cout << "Yes, Yousef can reach the Koshary!" << endl;
} else {
cout << "No, the Koshary is out of reach." << endl;
}
return 0;
}-
Time Complexity:
$O(1)$ , as we are performing basic arithmetic and parity checks. -
Space Complexity:
$O(1)$ , as we only use a few variables regardless of the input size.
| Result | Reason | |
|---|---|---|
(4, 6) |
Yes | Both even, 0 short steps needed. |
(4, 7) |
Yes | One odd, 1 short step needed. |
(5, 6) |
Yes | One odd, 1 short step needed. |
(5, 5) |
No | Both odd, 2 short steps needed. |
(-2, 4) |
No | Negative coordinate. |