

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to check we can buy product with given money or not
Suppose we have a number N. A cake seller is selling cakes with 40 rupees, and doughnuts with 70 rupees each. We have to check whether we can buy some of them with exactly N rupees or not.
So, if the input is like N = 110, then the output will be True, because 40 + 70 = 110.
To solve this, we will follow these steps −
o := false Define a function dfs(), this will take i, if i > n, then: return false if i is same as n, then: return true if dfs(i + 40), then: return true return dfs(i + 70) From the main method, do the following n := N o := dfs(0) return o
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int n; bool o = false; bool dfs(int i) { if (i > n) return false; if (i == n) return true; if (dfs(i + 40)) return true; return dfs(i + 70); } bool solve(int N) { n = N; o = dfs(0); return o; } int main(){ int N = 110; cout << solve(N) << endl; }
Input
110
Output
1
- Related Questions & Answers
- C++ Program to check given candies can be split with equal weights or not
- Python program to check whether we can pile up cubes or not
- Program to check we can reach leftmost or rightmost position or not in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check we can cross river by stones or not in Python
- Program to check we can form array from pieces or not in Python
- Program to check whether we can get N queens solution or not in Python
- C++ program to check we can rearrange array in such a way that given formula returns m or not
- Program to check we can visit any city from any city or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check we can reach at position n by jumping or not in Python
- Program to check whether we can make group of two partitions with equal sum or not in Python?
- Program to check given string is pangram or not in Python
Advertisements