Generation of n numbers with given set of factors

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to generate n numbers with
// given factors
#include<bits/stdc++.h>
using namespace std;
  
// Generate n numbers with factors in factor[]
void generateNumbers(int factor[], int n, int k)
{
    // array of k to store next multiples of
    // given factors 
    int next[k] = {0};
  
    // Prints n numbers  
    int output = 0;  // Next number to print as output 
    for (int i=0; i<n; )
    {
        // Find the next smallest multiple
        int toincrement = 0;
        for (int j=0; j<k; j++)
            if (next[j] < next[toincrement])
                toincrement = j;
  
        // Printing minimum in each iteration
        // print the value if output is not equal to
        // current value(to avoid the duplicates)
        if (output != next[toincrement])
        {
            output = next[toincrement];
            printf("%d ",  next[toincrement]);
            i++;
        }
  
        // incrementing the current value by the
        // respective factor
        next[toincrement] += factor[toincrement];
    }
}
  
// Driver code
int main()
{
    int factor[] = {3, 5, 7};
    int n = 10;
    int k = sizeof(factor)/sizeof(factor[0]);
    generateNumbers(factor, n, k);
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to generate
// n numbers with given factors
import java.io.*;
  
class GFG 
{
      
// Generate n numbers with
// factors in factor[]
static void generateNumbers(int factor[],
                            int n, int k)
{
    // array of k to store 
    // next multiples of 
    // given factors 
    int next[] = new int[k];
  
    // Prints n numbers 
    int output = 0; // Next number to
                    // print as output 
    for (int i = 0; i < n;)
    {
        // Find the next 
        // smallest multiple
        int toincrement = 0;
        for (int j = 0; j < k; j++)
            if (next[j] < next[toincrement])
                toincrement = j;
  
        // Printing minimum in 
        // each iteration print 
        // the value if output 
        // is not equal to current
        // value(to avoid the 
        // duplicates)
        if (output != next[toincrement])
        {
            output = next[toincrement];
            System.out.print(
                   next[toincrement] + " ");
            i++;
        }
  
        // incrementing the 
        // current value by the
        // respective factor
        next[toincrement] += 
               factor[toincrement];
    }
}
  
// Driver code
public static void main (String[] args) 
{
    int factor[] = {3, 5, 7};
    int n = 10;
    int k = factor.length;
    generateNumbers(factor, n, k);
}
}
  
// This code is contributed 
// by ajit
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to generate n 
# numbers with given factors
  
# Generate n numbers with 
# factors in factor[]
def generateNumbers(factor, n, k):
      
    # array of k to store next 
    # multiples of given factors 
    next = [0] * k;
  
    # Prints n numbers 
    output = 0; # Next number to
                # print as output 
    i = 0;
    while(i < n):
          
        # Find the next smallest 
        # multiple
        toincrement = 0;
        for j in range(k):
            if(next[j] < next[toincrement]):
                toincrement = j;
  
        # Printing minimum in each 
        # iteration print the value
        # if output is not equal to
        # current value(to avoid the
        # duplicates)
        if(output != next[toincrement]):
            output = next[toincrement];
            print(next[toincrement], end = " ");
            i += 1;
  
        # incrementing the current 
        # value by the respective factor
        next[toincrement] += factor[toincrement];
  
# Driver code
factor = [3, 5, 7];
n = 10;
k = len(factor);
generateNumbers(factor, n, k);
      
# This code is contributed by mits
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to generate
// n numbers with given factors
using System;
  
class GFG
{
      
// Generate n numbers with
// factors in factor[]
static void generateNumbers(int []factor,
                            int n, int k)
{
    // array of k to store 
    // next multiples of 
    // given factors 
    int []next = new int[k];
  
    // Prints n numbers 
    int output = 0; // Next number to
                    // print as output 
    for (int i = 0; i < n;)
    {
        // Find the next 
        // smallest multiple
        int toincrement = 0;
        for (int j = 0; j < k; j++)
            if (next[j] < next[toincrement])
                toincrement = j;
  
        // Printing minimum in 
        // each iteration print 
        // the value if output 
        // is not equal to current
        // value(to avoid the 
        // duplicates)
        if (output != next[toincrement])
        {
            output = next[toincrement];
            Console.Write(
                next[toincrement] + " ");
            i++;
        }
  
        // incrementing the 
        // current value by the
        // respective factor
        next[toincrement] += 
            factor[toincrement];
    }
}
  
// Driver code
static public void Main ()
{
    int []factor = {3, 5, 7};
    int n = 10;
    int k = factor.Length;
    generateNumbers(factor, n, k);
}
}
  
// This code is contributed 
// by akt_mit
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to generate n numbers
// with given factors
  
// Generate n numbers with factors 
// in factor[]
function generateNumbers($factor, $n, $k)
{
    // array of k to store next 
    // multiples of given factors 
    $next = array_fill(0, $k, 0);
  
    // Prints n numbers 
    $output = 0; // Next number to print 
                 // as output 
    for ($i = 0; $i < $n; )
    {
        // Find the next smallest multiple
        $toincrement = 0;
        for ($j = 0; $j < $k; $j++)
            if ($next[$j] < $next[$toincrement])
                $toincrement = $j;
  
        // Printing minimum in each iteration
        // print the value if output is not 
        // equal to current value(to avoid 
        // the duplicates)
        if ($output != $next[$toincrement])
        {
            $output = $next[$toincrement];
            echo $next[$toincrement] . " ";
            $i++;
        }
  
        // incrementing the current value 
        // by the respective factor
        $next[$toincrement] += $factor[$toincrement];
    }
}
  
// Driver code
$factor = array(3, 5, 7);
$n = 10;
$k = count($factor);
generateNumbers($factor, $n, $k);
  
// This code is contributed by mits
?>
chevron_right


Output :
3 5 6 7 9 10 12 14 15 18 

Time complexity – O(n * k)
Auxiliary Space – O(k)

This article is contributed by Niteesh kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Improved By : jit_t, Mithun Kumar



Article Tags :
Practice Tags :