Check if N can be obtained from 0 using at most X increment and at most Y doubling operations
Last Updated :
21 Feb, 2022
Given three integers N, X and Y, the task is to check if N can be obtained from 0 using the following operations:
- The current integer can be incremented by one (i.e., x = x + 1) and this can be done at most X times.
- Double the current integer (i.e., x = 2 * x) and this can be done at most Y times.
Return false if the number cannot be reached.
Examples:
Input: N = 24, X = 6 ,Y = 2
Output: true
Explanation: Initially, a = 0
Increment once so a = 1
Increment once so a = 2
Increment once so a = 3
Increment once so a = 4
Increment once so a = 5
Increment once so a = 6
Double once so a = 12
Double again so a = 24
Input: N = 4, X = 2 ,Y = 0
Output: false
Approach: The question can also be considered in the exact opposite scenario, where N is given and we need to reduce it to 0 by using two operations :
- Dividing target by 2 as many as maxDoubles times
- Decrementing target by 1 as many as maxadd times.
Use recursive function in this way to check if 0 can be obtained or not. In each recursive scenario decrement 1 and call the function again. And if the number is even divide it by 2 and call the recursive function. If 0 can be obtained for within the given limit of moves return true. Otherwise, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checktogetTarget( int N, int X, int Y)
{
if (N == 0)
return true ;
if (Y == 0 && X == 0)
return false ;
int temp = N;
while (1) {
if (temp % 2 == 0 && Y != 0) {
temp = temp / 2;
Y--;
}
else if (X != 0) {
temp--;
X--;
}
if (temp == 0)
break ;
if (Y == 0 && X == 0)
break ;
}
if (temp == 0) {
return true ;
}
else {
return false ;
}
}
int main()
{
int N = 24;
int X = 6;
int Y = 2;
bool ans = checktogetTarget(N, X, Y);
if (ans)
cout << "true" ;
else
cout << "false" ;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean checktogetTarget( int N, int X, int Y)
{
if (N == 0 )
return true ;
if (Y == 0 && X == 0 )
return false ;
int temp = N;
while ( true ) {
if (temp % 2 == 0 && Y != 0 ) {
temp = temp / 2 ;
Y--;
}
else if (X != 0 ) {
temp--;
X--;
}
if (temp == 0 )
break ;
if (Y == 0 && X == 0 )
break ;
}
if (temp == 0 ) {
return true ;
}
else {
return false ;
}
}
public static void main (String[] args) {
int N = 24 ;
int X = 6 ;
int Y = 2 ;
Boolean ans = checktogetTarget(N, X, Y);
if (ans)
System.out.print( "true" );
else
System.out.print( "false" );
}
}
|
Python3
def checktogetTarget(N, X, Y):
if (N = = 0 ):
return True ;
if (Y = = 0 and X = = 0 ):
return False ;
temp = N;
while ( 1 ):
if (temp % 2 = = 0 and Y ! = 0 ):
temp = temp / 2 ;
Y - = 1
elif (X ! = 0 ):
temp - = 1
X - = 1
if (temp = = 0 ):
break ;
if (Y = = 0 and X = = 0 ):
break ;
if (temp = = 0 ):
return True
else :
return False
N = 24 ;
X = 6 ;
Y = 2 ;
ans = checktogetTarget(N, X, Y);
if (ans):
print ( "true" );
else :
print ( "false" );
|
C#
using System;
class GFG
{
static bool checktogetTarget( int N, int X, int Y)
{
if (N == 0)
return true ;
if (Y == 0 && X == 0)
return false ;
int temp = N;
while ( true ) {
if (temp % 2 == 0 && Y != 0) {
temp = temp / 2;
Y--;
}
else if (X != 0) {
temp--;
X--;
}
if (temp == 0)
break ;
if (Y == 0 && X == 0)
break ;
}
if (temp == 0) {
return true ;
}
else {
return false ;
}
}
public static void Main()
{
int N = 24;
int X = 6;
int Y = 2;
bool ans = checktogetTarget(N, X, Y);
if (ans)
Console.Write( "true" );
else
Console.Write( "false" );
}
}
|
Javascript
<script>
function checktogetTarget(N, X, Y) {
if (N == 0)
return true ;
if (Y == 0 && X == 0)
return false ;
let temp = N;
while (1) {
if (temp % 2 == 0 && Y != 0) {
temp = temp / 2;
Y--;
}
else if (X != 0) {
temp--;
X--;
}
if (temp == 0)
break ;
if (Y == 0 && X == 0)
break ;
}
if (temp == 0) {
return true ;
}
else {
return false ;
}
}
let N = 24;
let X = 6;
let Y = 2;
let ans = checktogetTarget(N, X, Y);
if (ans)
document.write( "true" );
else
document.write( "false" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...