Check if N can be represented as sum of integers chosen from set {A, B}

Given three integers N, A and B, the task is to find whether N can be represented as sum of A’s and B’s.
Examples:
Input: N = 11, A = 2, B = 3
Output: Yes
2 + 2 + 2 + 2 + 3 = 11Input: N = 8, A = 3, B = 7
Output: No
Approach: An efficient solution is to call a recursive function starting with zero (because zero is always possible). If function call is fun(x) then recursively call fun(x + a) and fun(x + b) (because if x is possible then x + a and x + b are also possible). Return out of the function if x > n.
Below is the implementation of the above approach:
C++
// CPP program to find if number N can// be represented as sum of a's and b's#include <bits/stdc++.h>using namespace std;// Function to find if number N can// be represented as sum of a's and b'svoid checkIfPossibleRec(int x, int a, int b, bool isPossible[], int n){ // base condition if (x > n) return; // if x is already visited if (isPossible[x]) return; // set x as possible isPossible[x] = true; // recursive call checkIfPossibleRec(x + a, a, b, isPossible, n); checkIfPossibleRec(x + b, a, b, isPossible, n);}bool checkPossible(int n, int a, int b){ bool isPossible[n + 1] = { false }; checkIfPossibleRec(0, a, b, isPossible, n); return isPossible[n];}// Driver programint main(){ int a = 3, b = 7, n = 8; if (checkPossible(a, b, n)) cout << "Yes"; else cout << "No"; return 0;} |
Java
// Java program to find if number N can// be represented as sum of a's and b'simport java.util.*;class solution{// Function to find if number N can// be represented as sum of a's and b'sstatic void checkIfPossibleRec(int x, int a, int b, boolean isPossible[], int n){ // base condition if (x > n) return; // if x is already visited if (isPossible[x]) return; // set x as possible isPossible[x] = true; // recursive call checkIfPossibleRec(x + a, a, b, isPossible, n); checkIfPossibleRec(x + b, a, b, isPossible, n);}static boolean checkPossible(int n, int a, int b){ boolean isPossible[]=new boolean[n + 1]; for(int i=0;i<=n;i++) isPossible[i]=false; checkIfPossibleRec(0, a, b, isPossible, n); return isPossible[n];}// Driver programpublic static void main(String args[]){ int a = 3, b = 7, n = 8; if (checkPossible(a, b, n)) System.out.print("Yes"); else System.out.print( "No");}}//contributed by Arnab Kundu |
Python3
# Python3 program to find if number N can # be represented as sum of a's and b's # Function to find if number N can # be represented as sum of a's and b's def checkIfPossibleRec(x, a, b, isPossible, n): # base condition if x > n: return # If x is already visited if isPossible[x]: return # Set x as possible isPossible[x] = True # Recursive call checkIfPossibleRec(x + a, a, b, isPossible, n) checkIfPossibleRec(x + b, a, b, isPossible, n) def checkPossible(n, a, b): isPossible = [False] * (n + 1) checkIfPossibleRec(0, a, b, isPossible, n) return isPossible[n] # Driver Codeif __name__ == "__main__": a, b, n = 3, 7, 8 if checkPossible(a, b, n): print("Yes") else: print("No") # This code is contributed by Rituraj Jain |
C#
// C# program to find if number N can // be represented as sum of a's and b's using System;class GFG{// Function to find if number N can // be represented as sum of a's and b's static void checkIfPossibleRec(int x, int a, int b, bool []isPossible, int n) { // base condition if (x > n) return; // if x is already visited if (isPossible[x]) return; // set x as possible isPossible[x] = true; // recursive call checkIfPossibleRec(x + a, a, b, isPossible, n); checkIfPossibleRec(x + b, a, b, isPossible, n); } static bool checkPossible(int n, int a, int b) { bool []isPossible = new bool[n + 1]; for(int i = 0; i <= n; i++) isPossible[i] = false; checkIfPossibleRec(0, a, b, isPossible, n); return isPossible[n]; } // Driver Code static public void Main (){ int a = 3, b = 7, n = 8; if (checkPossible(a, b, n)) Console.WriteLine("Yes"); else Console.WriteLine( "No"); }}// This code is contributed by Sach_Code |
PHP
<?php// PHP program to find if number N can// be represented as sum of a's and b's// Function to find if number N can// be represented as sum of a's and b'sfunction checkIfPossibleRec($x, $a, $b, $isPossible, $n){ // base condition if ($x > $n) return; // if x is already visited if ($isPossible == true) return; // set x as possible $isPossible[$x] = true; // recursive call checkIfPossibleRec($x + $a, $a, $b, $isPossible, $n); checkIfPossibleRec($x + $b, $a, $b, $isPossible, $n);}function checkPossible($n, $a, $b){ $isPossible[$n + 1] = array(false); checkIfPossibleRec(0, $a, $b, $isPossible, $n); return $isPossible;}// Driver Code$a = 3;$b = 7;$n = 8;if (checkPossible($a, $b, $n)) echo "No";else echo "Yes";// This code is contributed by Sach_Code?> |
Javascript
<script>// Javascript program to find if number N can// be represented as sum of a's and b's // Function to find if number N can// be represented as sum of a's and b'sfunction checkIfPossibleRec(x, a, b, isPossible, n){ // Base condition if (x > n) return; // If x is already visited if (isPossible[x]) return; // Set x as possible isPossible[x] = true; // Recursive call checkIfPossibleRec(x + a, a, b, isPossible, n); checkIfPossibleRec(x + b, a, b, isPossible, n);}function checkPossible(n, a, b) { var isPossible = Array(n + 1).fill(false); checkIfPossibleRec(0, a, b, isPossible, n); return isPossible[n];}// Driver codevar a = 3, b = 7, n = 8;if (checkPossible(a, b, n)) document.write("Yes");else document.write("No");// This code is contributed by todaysgaurav </script> |
Output:
No
Time Complexity: O(2^n) , recursive function time complexity
Auxiliary Space: O(n), as extra space of size (n+1) is used to make a boolean array
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


