Count numbers which can be constructed using two numbers
Last Updated :
16 Oct, 2023
Given three positive integers x, y and n, the task is to find count of all numbers from 1 to n that can be formed using x and y. A number can be formed using x and y if we can get it by adding any number of occurrences of x and/or y.
Examples :
Input : n = 10, x = 2, y = 3
Output : 9
We can form 9 out of 10 numbers using 2 and 3
2 = 2, 3 = 3, 4 = 2+2, 5 = 2+3, 6 = 3+3
7 = 2+2+3, 8 = 3+3+2, 9 = 3+3+3
and 10 = 3+3+2+2.
Input : n = 10, x = 5, y = 7
Output : 3
We can form 3 out of 10 numbers using 5 and 7
The numbers are 5, 7 and 10
Input : n = 15, x = 5, y = 7
Output : 6
We can form 6 out of 10 numbers using 5 and 7.
The numbers are 5, 7, 10, 12, 14 and 15.
Input : n = 15, x = 2, y = 4
Output : 7
A simple solution is to write a recursive code that starts with 0 and makes two recursive calls. One recursive call adds x and other adds y. This way we count total numbers. We need to make sure a number is counted multiple times.
An efficient solution solution is to use a boolean array arr[] of size n+1. An entry arr[i] = true is going to mean that i can be formed using x and y. We initialize arr[x] and arr[y] as true if x and y are smaller than or equal to n. We start traversing the array from smaller of two numbers and mark all numbers one by one that can be formed using x and y.
Below is the implementation.
C++
#include<bits/stdc++.h>
using namespace std;
int countNums( int n, int x, int y)
{
vector< bool > arr(n+1, false );
if (x <= n)
arr[x] = true ;
if (y <= n)
arr[y] = true ;
int result = 0;
for ( int i=min(x, y); i<=n; i++)
{
if (arr[i])
{
if (i+x <= n)
arr[i+x] = true ;
if (i+y <= n)
arr[i+y] = true ;
result++;
}
}
return result;
}
int main()
{
int n = 15, x = 5, y = 7;
cout << countNums(n, x, y);
return 0;
}
|
Java
class gfg{
static int countNums( int n, int x, int y)
{
boolean [] arr= new boolean [n+ 1 ];
if (x <= n)
arr[x] = true ;
if (y <= n)
arr[y] = true ;
int result = 0 ;
for ( int i=Math.min(x, y); i<=n; i++)
{
if (arr[i])
{
if (i+x <= n)
arr[i+x] = true ;
if (i+y <= n)
arr[i+y] = true ;
result++;
}
}
return result;
}
public static void main(String[] args)
{
int n = 15 , x = 5 , y = 7 ;
System.out.println(countNums(n, x, y));
}
}
|
Python3
def countNums(n, x, y):
arr = [ False for i in range (n + 2 )]
if (x < = n):
arr[x] = True
if (y < = n):
arr[y] = True
result = 0
for i in range ( min (x, y), n + 1 ):
if (arr[i]):
if (i + x < = n):
arr[i + x] = True
if (i + y < = n):
arr[i + y] = True
result = result + 1
return result
n = 15
x = 5
y = 7
print (countNums(n, x, y))
|
C#
using System;
public class GFG{
static int countNums( int n, int x, int y)
{
bool []arr= new bool [n+1];
if (x <= n)
arr[x] = true ;
if (y <= n)
arr[y] = true ;
int result = 0;
for ( int i=Math.Min(x, y); i<=n; i++)
{
if (arr[i])
{
if (i+x <= n)
arr[i+x] = true ;
if (i+y <= n)
arr[i+y] = true ;
result++;
}
}
return result;
}
static public void Main (){
int n = 15, x = 5, y = 7;
Console.WriteLine(countNums(n, x, y));
}
}
|
Javascript
<script>
function countNums(n, x, y)
{
arr= Array(n+1).fill( false );
if (x <= n)
arr[x] = true ;
if (y <= n)
arr[y] = true ;
var result = 0;
for (i = Math.min(x, y); i <= n; i++)
{
if (arr[i])
{
if (i + x <= n)
arr[i + x] = true ;
if (i + y <= n)
arr[i + y] = true ;
result++;
}
}
return result;
}
var n = 15, x = 5, y = 7;
document.write(countNums(n, x, y));
</script>
|
PHP
<?php
function countNums( $n , $x , $y )
{
$arr = array_fill (0, $n + 1, false);
if ( $x <= $n )
$arr [ $x ] = true;
if ( $y <= $n )
$arr [ $y ] = true;
$result = 0;
for ( $i = min( $x , $y ); $i <= $n ; $i ++)
{
if ( $arr [ $i ])
{
if ( $i + $x <= $n )
$arr [ $i + $x ] = true;
if ( $i + $y <= $n )
$arr [ $i + $y ] = true;
$result ++;
}
}
return $result ;
}
$n = 15;
$x = 5;
$y = 7;
echo countNums( $n , $x , $y );
?>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach 2: Using DP:
This code implements a dynamic programming approach to solve a problem of counting the number of integers that can be formed using two given integers x and y, up to a given integer n.
- The function countNumsDP takes four arguments: n, x, y, and a reference to a vector called dp. The integer n is the upper limit up to which the numbers need to be counted, and x and y are the two given integers that can be used to form other integers. The vector dp is used to store the results of subproblems, which can be reused in later computations to avoid redundant computations.
- The function first checks if the base case n=0 is reached, and returns 0. If the result for n has already been computed and stored in dp, then it simply returns the result without performing further computations.
- The function then creates a boolean array called arr, where arr[i] represents whether the number i can be formed using x and y or not. It sets arr[x] and arr[y] to true if x and y are less than or equal to n.
- Next, the function iterates over all numbers from min(x,y) to n, and if the number i can be formed using x and y, it checks whether i+x and i+y can also be formed using x and y. If they can be formed, the function increments the result counter and sets arr[i+x] and arr[i+y] to true.
- Finally, it stores the result in dp[n] and returns the result.
This approach utilizes the previously computed results stored in the dp table to avoid redundant computations, thus improving the efficiency of the algorithm.
C++
#include<bits/stdc++.h>
using namespace std;
int countNumsDP( int n, int x, int y, vector< int >& dp) {
if (n == 0) return 0;
if (dp[n] != -1) return dp[n];
vector< bool > arr(n+1, false );
if (x <= n) arr[x] = true ;
if (y <= n) arr[y] = true ;
int result = 0;
for ( int i=min(x, y); i<=n; i++) {
if (arr[i]) {
if (i+x <= n) arr[i+x] = true ;
if (i+y <= n) arr[i+y] = true ;
result++;
}
}
dp[n] = result;
return result;
}
int main() {
int n = 15, x = 5, y = 7;
vector< int > dp(n+1, -1);
cout << countNumsDP(n, x, y, dp) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int countNumsDP( int n, int x, int y, int [] dp) {
if (n == 0 )
return 0 ;
if (dp[n] != - 1 )
return dp[n];
boolean [] arr = new boolean [n + 1 ];
if (x <= n)
arr[x] = true ;
if (y <= n)
arr[y] = true ;
int result = 0 ;
for ( int i = Math.min(x, y); i <= n; i++) {
if (arr[i]) {
if (i + x <= n)
arr[i + x] = true ;
if (i + y <= n)
arr[i + y] = true ;
result++;
}
}
dp[n] = result;
return result;
}
public static void main(String[] args) {
int n = 15 , x = 5 , y = 7 ;
int [] dp = new int [n + 1 ];
Arrays.fill(dp, - 1 );
System.out.println(countNumsDP(n, x, y, dp));
}
}
|
Python3
def countNumsDP(n, x, y, dp):
if n = = 0 :
return 0
if dp[n] ! = - 1 :
return dp[n]
arr = [ False ] * (n + 1 )
if x < = n:
arr[x] = True
if y < = n:
arr[y] = True
result = 0
for i in range ( min (x, y), n + 1 ):
if arr[i]:
if i + x < = n:
arr[i + x] = True
if i + y < = n:
arr[i + y] = True
result + = 1
dp[n] = result
return result
def main():
n = 15
x = 5
y = 7
dp = [ - 1 ] * (n + 1 )
print (countNumsDP(n, x, y, dp))
if __name__ = = "__main__" :
main()
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int CountNumsDP( int n, int x, int y, List< int > dp) {
if (n == 0) return 0;
if (dp[n] != -1) return dp[n];
List< bool > arr = new List< bool >(n + 1);
for ( int i = 0; i <= n; i++) {
arr.Add( false );
}
if (x <= n) arr[x] = true ;
if (y <= n) arr[y] = true ;
int result = 0;
for ( int i = Math.Min(x, y); i <= n; i++) {
if (arr[i]) {
if (i + x <= n) arr[i + x] = true ;
if (i + y <= n) arr[i + y] = true ;
result++;
}
}
dp[n] = result;
return result;
}
public static void Main( string [] args) {
int n = 15, x = 5, y = 7;
List< int > dp = new List< int >(n + 1);
for ( int i = 0; i <= n; i++) {
dp.Add(-1);
}
Console.WriteLine(CountNumsDP(n, x, y, dp));
}
}
|
Javascript
function countNumsDP(n, x, y, dp) {
if (n === 0) return 0;
if (dp[n] !== -1) return dp[n];
const arr = new Array(n + 1).fill( false );
if (x <= n) arr[x] = true ;
if (y <= n) arr[y] = true ;
let result = 0;
for (let i = Math.min(x, y); i <= n; i++) {
if (arr[i]) {
if (i + x <= n) arr[i + x] = true ;
if (i + y <= n) arr[i + y] = true ;
result++;
}
}
dp[n] = result;
return result;
}
function main() {
const n = 15, x = 5, y = 7;
const dp = new Array(n + 1).fill(-1);
console.log(countNumsDP(n, x, y, dp));
}
main();
|
Time complexity: O(n * min(x, y))
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...