Final Destination | Practice | GeeksforGeeks

Consider a 2d plane and a Robot which is located at (0,0) who can move only one unit step at a time in any direction i.e. if its initial position is (x,y), he can go to positions (x + 1, y), (x - 1, y), (x, y + 1) or (x, y - 1). Now Given three integers a,b (denoting the final position where the robot has to reach), and x. Find out if the Robot can reach the final position in exactly x steps.
 
Example 1:
Input:
a = 5, b = 5, x = 11
Output:
0
Explanation:
No matter how the Robot moves,
the Robot won't be able to reach
point (5,5) in exactly 11 moves.
Example 2:
Input:
a = 10, b = 15, x = 25
Output:
1
Explanation:
The Robot can move 10 times towards
positive x-axis and then 15 times
towards positive y-axis to reach (10,15).

 
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes 3 Integers a,b and x as input and returns the answer.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
-109 <= a,b <= 109
1 <= x <= 2*109
0
badgujarsachin831 week ago
int canReach(long long a, long long b, long long x) {
        // code here
        int sum=abs(a)+abs(b);
        if(x<sum){
            return 0;
        }
        int di=x-sum;
        return di%2==0;
    }
0
gaurav79161 week ago
   int canReach(long long a, long long b, long long x) {
       if(abs(a)+abs(b)<=x && (x-(a+b))%2==0)
           return 1;
       else
           return 0;
   }
+1
bhaskarmaheshwari83 weeks ago
     // second last cell pe jaane ke  baad agr  even steps  hue toh apn kitne bhi round marke aa skte draw u will understand       long long min_moves = abs(a)+abs(b);       if(x<min_moves) {           return 0;       }       long long diff = x-min_moves;       return diff%2==0;
0
bhaskarmaheshwari84 weeks ago
what is wrong in this code??
i have applied dfs on grid
 
 bool isSafe(int row,int col,int m,int n)     {         if(row<0||col<0||row>m||col>n)         return 0;         return 1;     }   int dfs(int a,int b,int x,int cnt,int row,int col,vector<vector<int>> &vis)   {       if((row==a&&col==b)&&cnt==x)       return 1;              vis[row][col]=1;       int dx[]={-1,1,0,0};       int dy[]={0,0,-1,1};       for(int i=0;i<4;i++)       {           if(isSafe(row+dx[i],col+dy[i],a,b)&&!vis[row+dx[i]][col+dy[i]])           {               if(dfs(a,b,x,cnt+1,row+dx[i],col+dy[i],vis))               return 1;           }       }       return 0;          }   int canReach(long long a, long long b, long long x) {       // code here       int cnt=0;       vector<vector<int>> vis(a+1,vector<int>(b+1,0));       return dfs(a,b,x,cnt,0,0,vis);   }
0
ervishnu19942 months ago
class Solution { public:   int canReach(long long a, long long b, long long x) {       if(a<0)a*=-1;       if(b<0)b*=-1;       if(x>=(a+b)&&(x-(a+b))%2==0)           return 1;       return 0;   }};
0
hemantkrkrayla6 months ago
C++ Solution
 
class Solution {
  public:
    int canReach(long long a, long long b, long long x) {
        // code here
        long long min_moves = abs(a)+abs(b);
        if(x<min_moves) {
            return 0;
        }
        long long diff = x-min_moves;
        return diff%2==0;
    }
};
0
code_prime6 months ago
int canReach(long long a, long long b, long long x) {       long long min_steps = abs(a)+abs(b);       if(x<min_steps)           return 0;       return (x-min_steps)%2==0;   }
0
muhammedrameez141220017 months ago
int canReach(long long a, long long b, long long x) {
      a=abs(a);
      b=abs(b);
      if(a+b>x)
      return 0;
      if(a+b==x)
      return 1;
      
      if((a+b)%2==0)
      return x%2==0?1:0;
      
      if((a+b)%2!=0)
      return x%2!=0?1:0;
   }
0
ANAS MALVAT7 months ago
ANAS MALVAT
        a  = abs(a);        b = abs(b);        if(a  + b > x )            return 0;        if(a + b == x)            return 1;        if((a + b) % 2 == 0 and x % 2 == 0)            return 1;        if((a + b) % 2 != 0 and x % 2 != 0)            return 1;        return 0;
0
Achintya Veer Singh8 months ago
Achintya Veer Singh
Java One-Liner:
return (x-Math.abs(a)-Math.abs(b))%2 == 0 && (x-Math.abs(a)-Math.abs(b)) >= 0? 1 : 0;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.