Given two integers N and M, the task is to check if it is possible to obtain these values from X = 1 and Y = 0 respectively by performing the two operations any number of times::
- Increase X and Y by 1, if and only if x>0.
- Increase Y by 2, if and only if y>0.
Input: N = 3, M = 4
Initially X = 1, Y = 0
Operation 1: X = 2, Y = 1
Operation 1: X = 3, Y = 2
Operation 2: X = 3, Y = 4, hence the final values are got so the answer is Yes.
Input: N = 5, M = 2
Obtaining X = 5 and Y = 2 from X = 1 and Y = 0 is not possible.
Approach: The above problem can be solved using the below observations:
- If N is less than 2 and M is not equal to zero, then getting the final values is not possible, hence the answer is No.
- Otherwise, subtract N from M and if M ? 0 and M is divisible by 2 then the answer is Yes.
- In all other cases, the answer is No.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Count squares possible from M and N straight lines parallel to X and Y axis respectively
- Count of Binary Strings possible as per given conditions
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Subsequence pair from given Array having all unique and all same elements respectively
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Highest and Smallest power of K less than and greater than equal to N respectively
- Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Maximum number of objects that can be created as per given conditions
- Maximize the last Array element as per the given conditions
- Arrange the numbers in the Array as per given inequalities
- Check if it is possible to reach a number by making jumps of two given length
- Check if it is possible to reach (X, Y) from (1, 0) by given steps
- Check if it is possible to reach (X, Y) from (1, 1) by given steps
- Time taken per hour for stoppage of Car
- Program to find the Speed of train as per speed of sound
- Check if it is possible to reach vector B by rotating vector A and adding vector C to it
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.