Print all numbers that can be obtained by adding A or B to N exactly M times
Last Updated :
15 Dec, 2021
Given four integers N, M, A, and B, the task is to print all numbers ( in increasing order ) that can be obtained by adding A or B to N exactly M times.
Examples:
Input: N = 5, M = 3, A = 4, B = 6
Output: 17 19 21 23
Explanation:
Step 1: Adding 4 or 6 to N generates 9 and 11.
Step 2: Adding 4 and 6 to N generates 13, 15, 17.
Step 3: Adding 4 and 6 to N generates 17, 19, 21, 23.
Input: N = 8, M = 2, A = 5, B = 10
Output: 18 23 28
Naive Approach: The simplest approach to solve this problem is to use Recursion. In each step, there are only two choices, i.e. to either add A or add B.
Follow the steps below to solve this problem:
- Initialize a Set, say numbers, to store all the numbers that can be made.
- Base case: If M is equal to 0, then the only possible number is N.
- After adding A to N, make a recursive call for M – 1 steps.
- After adding B to N, make a recursive call for M – 1 steps.
- Finally, iterate over the set numbers and print all the numbers.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void possibleNumbers(set< int >& numbers,
int N, int M,
int A, int B)
{
if (M == 0) {
numbers.insert(N);
return ;
}
possibleNumbers(numbers, N + A, M - 1, A, B);
possibleNumbers(numbers, N + B, M - 1, A, B);
}
int main()
{
int N = 5, M = 3, A = 4, B = 6;
set< int > numbers;
possibleNumbers(numbers, N, M, A, B);
for ( int x : numbers) {
cout << x << " " ;
}
return 0;
}
|
Java
import java.util.*;
public class MyClass{
static void possibleNumbers(Set<Integer> numbers, int N, int M, int A, int B)
{
if (M == 0 ) {
numbers.add(N);
return ;
}
possibleNumbers(numbers, N + A, M - 1 , A, B);
possibleNumbers(numbers, N + B, M - 1 , A, B);
}
public static void main(String args[])
{
int N = 5 , M = 3 , A = 4 , B = 6 ;
Set<Integer> numbers = new HashSet<Integer>();
possibleNumbers(numbers, N, M, A, B);
Iterator<Integer> i = numbers.iterator();
while (i.hasNext())
System.out.print(i.next()+ " " );
}}
|
Python3
def possibleNumbers(numbers, N, M, A, B):
if (M = = 0 ):
numbers.add(N)
return
possibleNumbers(numbers, N + A,
M - 1 , A, B)
possibleNumbers(numbers, N + B,
M - 1 , A, B)
if __name__ = = '__main__' :
N = 5
M = 3
A = 4
B = 6
numbers = set ()
possibleNumbers(numbers, N, M, A, B)
for x in numbers:
print (x, end = " " )
|
C#
using System;
using System.Collections.Generic;
public class MyClass {
static void possibleNumbers(HashSet< int > numbers, int N,
int M, int A, int B)
{
if (M == 0) {
numbers.Add(N);
return ;
}
possibleNumbers(numbers, N + A, M - 1, A, B);
possibleNumbers(numbers, N + B, M - 1, A, B);
}
public static void Main(String[] args)
{
int N = 5, M = 3, A = 4, B = 6;
HashSet< int > numbers = new HashSet< int >();
possibleNumbers(numbers, N, M, A, B);
foreach ( int i in numbers) Console.Write(i + " " );
}
}
|
Javascript
<script>
function possibleNumbers(numbers, N, M, A, B) {
if (M == 0) {
numbers.add(N);
return ;
}
possibleNumbers(numbers, N + A, M - 1, A, B);
possibleNumbers(numbers, N + B, M - 1, A, B);
}
var N = 5, M = 3, A = 4, B = 6;
var numbers = new Set();
possibleNumbers(numbers, N, M, A, B);
for (let x of numbers) {
document.write(x+ ' ' );
}
</script>
|
Time Complexity: O(2M)
Auxiliary Space: O(M)
Efficient Approach: This problem has Overlapping Subproblems property and Optimal Substructure property. Therefore, the above approach can be optimized using Dynamic Programming.
Follow the steps below to solve this problem:
- Initialize a vector, say dp[][], and a set, say numbers, where dp[i][j] stores whether the number j is visited or not in i steps.
- Base case: If M is equal to 0, then the only possible number is N.
- If (N+A) has not been obtained at (M-1)th step previously, then after adding A to N, make a recursive call for M – 1 steps and mark dp[M – 1][N + A] as visited.
- If (N + B) has not been obtained at (M – 1)th step previously, then after adding B to N, make a recursive call for M – 1 steps and mark dp[M – 1][N + B] as visited.
- Finally, iterate over the set numbers and print all the numbers.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
void possibleNumbers( int N, int M,
int A, int B)
{
if (A > B) {
swap(A, B);
}
int number = N + M * A;
cout << number << " " ;
if (A != B) {
for ( int i = 0; i < M; i++) {
number = number - A + B;
cout << number << " " ;
}
}
}
int main()
{
int N = 5, M = 3, A = 4, B = 6;
possibleNumbers(N, M, A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void possibleNumbers( int N, int M,
int A, int B)
{
if (A > B)
{
int temp = A;
A = B;
B = temp;
}
int number = N + M * A;
System.out.print(number + " " );
if (A != B)
{
for ( int i = 0 ; i < M; i++)
{
number = number - A + B;
System.out.print(number + " " );
}
}
}
public static void main(String[] args)
{
int N = 5 , M = 3 , A = 4 , B = 6 ;
possibleNumbers(N, M, A, B);
}
}
|
Python3
def possibleNumbers(N, M, A, B):
if (A > B):
temp = A
A = B
B = temp
number = N + M * A
print (number, end = " " )
if (A ! = B):
for i in range (M):
number = number - A + B
print (number,end = " " )
if __name__ = = '__main__' :
N = 5
M = 3
A = 4
B = 6
possibleNumbers(N, M, A, B)
|
C#
using System;
class GFG{
static void possibleNumbers( int N, int M, int A, int B)
{
if (A > B)
{
int temp = A;
A = B;
B = temp;
}
int number = N + M * A;
Console.Write(number + " " );
if (A != B)
{
for ( int i = 0; i < M; i++)
{
number = number - A + B;
Console.Write(number + " " );
}
}
}
public static void Main(String[] args)
{
int N = 5, M = 3, A = 4, B = 6;
possibleNumbers(N, M, A, B);
}
}
|
Javascript
<script>
function possibleNumbers(N,M,A,B)
{
var i;
if (A > B) {
var temp = A;
A = B;
B = temp;
}
var number = N + M * A;
document.write(number + " " );
if (A != B) {
for (i = 0; i < M; i++) {
number = number - A + B;
document.write(number + " " );
}
}
}
var N = 5, M = 3, A = 4, B = 6;
possibleNumbers(N, M, A, B);
</script>
|
Time Complexity: O(M)
Auxiliary Space: O(M*105)
Efficient Approach: The above approach can be further optimized greedily. Follow the steps below to solve this problem:
- If A is greater than B, then swap A and B for maintaining increasing order.
- Initialize a variable, say number as N + M * A, i.e. the smallest number that can be achieved, and print the number.
- If A is not equal to B, then iterate over the range [0, M – 1] using a variable i, and print number – A + B.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void possibleNumbers( int N, int M,
int A, int B)
{
if (A > B) {
swap(A, B);
}
int number = N + M * A;
cout << number << " " ;
if (A != B) {
for ( int i = 0; i < M; i++) {
number = number - A + B;
cout << number << " " ;
}
}
}
int main()
{
int N = 5, M = 3, A = 4, B = 6;
possibleNumbers(N, M, A, B);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void possibleNumbers( int N, int M,
int A, int B)
{
if (A > B)
{
int temp = A;
A = B;
B = temp;
}
int number = N + M * A;
System.out.print(number + " " );
if (A != B) {
for ( int i = 0 ; i < M; i++) {
number = number - A + B;
System.out.print(number + " " );
}
}
}
public static void main(String[] args)
{
int N = 5 , M = 3 , A = 4 , B = 6 ;
possibleNumbers(N, M, A, B);
}
}
|
Python3
def possibleNumbers(N, M, A, B):
if (A > B):
temp = A
A = B
B = temp
number = N + M * A
print (number,end = " " )
if (A ! = B):
for i in range (M):
number = number - A + B
print (number,end = " " )
if __name__ = = '__main__' :
N = 5
M = 3
A = 4
B = 6
possibleNumbers(N, M, A, B)
|
C#
using System;
class GFG
{
static void possibleNumbers( int N, int M,
int A, int B)
{
if (A > B)
{
int temp = A;
A = B;
B = temp;
}
int number = N + M * A;
Console.Write(number + " " );
if (A != B) {
for ( int i = 0; i < M; i++) {
number = number - A + B;
Console.Write(number + " " );
}
}
}
public static void Main(String[] args)
{
int N = 5, M = 3, A = 4, B = 6;
possibleNumbers(N, M, A, B);
}
}
|
Javascript
<script>
function possibleNumbers( N, M, A, B)
{
if (A > B)
{
var temp = A;
A = B;
B = temp;
}
var number = N + M * A;
document.write(number + " " );
if (A != B) {
for ( var i = 0; i < M; i++) {
number = number - A + B;
document.write(number + " " );
}
}
}
var N = 5, M = 3, A = 4, B = 6;
possibleNumbers(N, M, A, B);
</script>
|
Time Complexity: O(M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...