Ninja and Bombs

Easy
0/40
0 upvote
Asked in company
MobiKwik

Problem statement

Ninja wants to travel from his house to his best friend’s house. The houses are located on a 2D coordinate plane, with Ninja’s house being situated at the origin. Ninja’s friend’s house is located somewhere on the x-axis. Now bombs are located at every point with an odd x-coordinate. Ninja can reach his friend’s house if it is an even coordinate else, he cannot reach there because there is a bomb placed at that point. There is a dragon who doesn’t let ninja use, modulus operator.

You are given an integer ‘N’ denoting the x-coordinate of Ninja’s friend’s house. Can Ninja reach his friend’s house? If yes, then return 1 else, return 0. Can you help him find without using the modulus operator?

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains ‘T’, denoting the number of test cases.

Each test case contains a single integer ‘N’, denoting the x-coordinate of Ninja’s friend’s house.
Output Format:
For each test case, return '1' if Ninja can reach his friend’s house else return '0'.
Note:
You cannot make use of the modulus operation.
You are not required to print the expected output; it has already been taken care of. 
Just implement the function.
Constraints:
1 <= T <= 10
1 <= N <= 10^9

Time Limit: 1 sec  
Sample Input 1 :
2
2
1
Sample Output 1 :
1
0   
Explanation for Sample Input 1 :
In the first test case, the x-coordinate of his friend’s house is, even so, Ninja can reach so 1 is returned.
In the second test case, the x-coordinate of his friend’s house is, odd so, Ninja can reach so 0 is returned.
Sample Input 2 :
2
7
2
Sample Output 2 :
0
1
Hint

Can you check whether a point is even or not?

Approaches (1)
Brute Force

The key here is to check whether a given number is even or not.

The steps are as follows:

  • If bitwise and of 1 and N is 1 then return 0 else return 1.
Time Complexity

O(1)

 

A mathematical operation was performed on the given input and no loop was used hence overall complexity is O(1).

Space Complexity

O(1)

 

As we are not using any extra space. Hence, the overall space complexity is O(1).

Code Solution
(100% EXP penalty)
Ninja and Bombs
Full screen
Console