Generation of n numbers with given set of factors
Last Updated :
28 Apr, 2021
Given an array of k numbers factor[], the task is to print first n numbers (in ascending order) whose factors are from the given array.
Examples:
Input : factor[] = {2, 3, 4, 7}
n = 8
Output : 2 3 4 6 7 8 9 10
Input : factor[] = {3, 5, 7}
n = 10
Output : 3 5 6 7 9 10 12 14 15 18
This problem is mainly an extension of Ugly Number Problem. We create an auxiliary array next[] of size same as factor[] to Keep track of next multiples of factors. In Each iteration, we output the minimum element from next, then increment it by the respective factor. If the minimum value is equal to the previous output value, increment it (to avoid duplicates). Else we print the minimum value.
C++
#include<bits/stdc++.h>
using namespace std;
void generateNumbers( int factor[], int n, int k)
{
int next[k] = {0};
int output = 0;
for ( int i=0; i<n; )
{
int toincrement = 0;
for ( int j=0; j<k; j++)
if (next[j] < next[toincrement])
toincrement = j;
if (output != next[toincrement])
{
output = next[toincrement];
printf ( "%d " , next[toincrement]);
i++;
}
next[toincrement] += factor[toincrement];
}
}
int main()
{
int factor[] = {3, 5, 7};
int n = 10;
int k = sizeof (factor)/ sizeof (factor[0]);
generateNumbers(factor, n, k);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void generateNumbers( int factor[],
int n, int k)
{
int next[] = new int [k];
int output = 0 ;
for ( int i = 0 ; i < n;)
{
int toincrement = 0 ;
for ( int j = 0 ; j < k; j++)
if (next[j] < next[toincrement])
toincrement = j;
if (output != next[toincrement])
{
output = next[toincrement];
System.out.print(
next[toincrement] + " " );
i++;
}
next[toincrement] +=
factor[toincrement];
}
}
public static void main (String[] args)
{
int factor[] = { 3 , 5 , 7 };
int n = 10 ;
int k = factor.length;
generateNumbers(factor, n, k);
}
}
|
Python3
def generateNumbers(factor, n, k):
next = [ 0 ] * k;
output = 0 ;
i = 0 ;
while (i < n):
toincrement = 0 ;
for j in range (k):
if ( next [j] < next [toincrement]):
toincrement = j;
if (output ! = next [toincrement]):
output = next [toincrement];
print ( next [toincrement], end = " " );
i + = 1 ;
next [toincrement] + = factor[toincrement];
factor = [ 3 , 5 , 7 ];
n = 10 ;
k = len (factor);
generateNumbers(factor, n, k);
|
C#
using System;
class GFG
{
static void generateNumbers( int []factor,
int n, int k)
{
int []next = new int [k];
int output = 0;
for ( int i = 0; i < n;)
{
int toincrement = 0;
for ( int j = 0; j < k; j++)
if (next[j] < next[toincrement])
toincrement = j;
if (output != next[toincrement])
{
output = next[toincrement];
Console.Write(
next[toincrement] + " " );
i++;
}
next[toincrement] +=
factor[toincrement];
}
}
static public void Main ()
{
int []factor = {3, 5, 7};
int n = 10;
int k = factor.Length;
generateNumbers(factor, n, k);
}
}
|
PHP
<?php
function generateNumbers( $factor , $n , $k )
{
$next = array_fill (0, $k , 0);
$output = 0;
for ( $i = 0; $i < $n ; )
{
$toincrement = 0;
for ( $j = 0; $j < $k ; $j ++)
if ( $next [ $j ] < $next [ $toincrement ])
$toincrement = $j ;
if ( $output != $next [ $toincrement ])
{
$output = $next [ $toincrement ];
echo $next [ $toincrement ] . " " ;
$i ++;
}
$next [ $toincrement ] += $factor [ $toincrement ];
}
}
$factor = array (3, 5, 7);
$n = 10;
$k = count ( $factor );
generateNumbers( $factor , $n , $k );
?>
|
Javascript
<script>
function generateNumbers(factor, n, k)
{
let next = new Array(k);
next.fill(0);
let output = 0;
for (let i = 0; i < n;)
{
let toincrement = 0;
for (let j = 0; j < k; j++)
if (next[j] < next[toincrement])
toincrement = j;
if (output != next[toincrement])
{
output = next[toincrement];
document.write(next[toincrement] + " " );
i++;
}
next[toincrement] += factor[toincrement];
}
}
let factor = [ 3, 5, 7 ];
let n = 10;
let k = factor.length;
generateNumbers(factor, n, k);
</script>
|
Output :
3 5 6 7 9 10 12 14 15 18
Time complexity – O(n * k)
Auxiliary Space – O(k)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...