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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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/C++

 // C++ program to generate n numbers with // given factors #include 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

Java

 // 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

Python3

 # 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 =  * 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

C#

 // 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

PHP



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.

My Personal Notes arrow_drop_up

Improved By : jit_t, Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.