You are a bank account hacker. Initially, you have 1 rupee in your account, and you want exactly N rupees in your account. You wrote two hacks, First hack can multiply the amount of money you own by 10, while the second can multiply it by 20. These hacks can be used at any number of times. Can you achieve the desired amount N using these hacks?
Example:
Input: n = 10
Output: Yes
Approach
C++
#include <bits/stdc++.h>using namespace std;bool hackMoney(long long n){if (n == 1)return true;if (n == 0)return false;if (n % 20 == 0)return hackMoney(n / 20) || hackMoney(n / 10);else if (n % 10 == 0)return hackMoney(n / 10);return false;}int main(){long long n = 10;bool ans = hackMoney(n);if (ans == true)cout << "Yes\n";elsecout << "No\n";}
Read Interview Questions
Exception Handling Interview Questions
DBMS Interview Questions Set -1
DBMS Interview Questions Set -2
JPA Interview Questions Set -1
Spring Boot Interview Questions Set 1
Spring Boot Interview Questions Set 2
No comments:
Post a Comment