Given a positive integer N, generate the first N positive integers that can be expressed in the form 2i * 3j * 5k * 7l where i, j, k, l ≥ 0 are integers. Find these first N numbers in strictly increasing order.
Examples:-
Input: N = 15
Output: 1 2 3 4 5 6 7 8 9 10 12 14 15 16 18
Explanation: All numbers from 1 to 18 except 11, 13 and 17 are of the given form.
Input: N = 1
Output: 1
Explanation: 1 is the first number of the desired sequence.
[Naive Approach] - By traversing and checking if the number is valid
The main idea of this approach is to iterate through all positive integers starting from 1 and check, for each number, whether its prime factors consist only of 2, 3, 5, and 7. For every number, we repeatedly divide it by these four primes until it is no longer divisible. If the number reduces to 1, it means it contained no other prime factors and is therefore considered valid. Each valid number is added to the result list, and this process continues until we collect n such numbers.
C++
//Driver Code Starts
#include <iostream>
#include <vector>
using namespace std;
//Driver Code Ends
bool isValid(long long curr) {
long long n = curr;
// 2, 3, 5 and 7 should only be the
// prime factors of a valid number
while (n % 2 == 0) n = n / 2;
while (n % 3 == 0) n = n / 3;
while (n % 5 == 0) n = n / 5;
while (n % 7 == 0) n = n / 7;
return n == 1;
}
vector<long long> generateNumbers(int N) {
vector<long long> nums;
for (long long i = 1; nums.size() < N; i++) {
// checking if the current number
// is of the given form
if (isValid(i)) {
// adding the number to the list
nums.push_back(i);
}
}
return nums;
}
//Driver Code Starts
int main() {
int n = 15;
vector<long long> ans = generateNumbers(n);
for (long long x : ans) cout << x << " ";
return 0;
}
//Driver Code Ends
Java
//Driver Code Starts
import java.util.ArrayList;
class GFG {
//Driver Code Ends
public static ArrayList<Long> generateNumbers(int N) {
ArrayList<Long> nums = new ArrayList<>();
for( long i = 1; nums.size() < N ; i++ ) {
// checking if the current number
// is of the given form
if( isValid(i)) {
// adding the number to the list
nums.add(i);
}
}
return nums;
}
public static boolean isValid( long curr ) {
long n = curr;
// 2, 3, 5 and 7 should only be the
// prime factors of a valid number
while( n % 2 == 0 ) n = n/2;
while( n % 3 == 0 ) n = n/3;
while( n % 5 == 0 ) n = n/5;
while( n % 7 == 0 ) n = n/7;
return n == 1;
}
//Driver Code Starts
public static void main(String[] args) {
int n = 15;
ArrayList<Long> nums = generateNumbers(n);
for( long num : nums)
System.out.print(num + " ");
System.out.println();
}
}
//Driver Code Ends
Python
def isValid(curr):
n = curr
# 2, 3, 5 and 7 should only be the
# prime factors of a valid number
while n % 2 == 0:
n //= 2
while n % 3 == 0:
n //= 3
while n % 5 == 0:
n //= 5
while n % 7 == 0:
n //= 7
return n == 1
def generateNumbers(N):
nums = []
i = 1
while len(nums) < N:
# checking if the current number
# is of the given form
if isValid(i):
# adding the number to the list
nums.append(i)
i += 1
return nums
#Driver Code Starts
if __name__ == "__main__":
n = 15
print(*generateNumbers(n))
#Driver Code Ends
C#
//Driver Code Starts
using System;
using System.Collections.Generic;
class GFG {
//Driver Code Ends
public static bool isValid(long curr) {
long n = curr;
// 2, 3, 5 and 7 should only be the
// prime factors of a valid number
while (n % 2 == 0) n = n / 2;
while (n % 3 == 0) n = n / 3;
while (n % 5 == 0) n = n / 5;
while (n % 7 == 0) n = n / 7;
return n == 1;
}
public static List<long> generateNumbers(int N) {
List<long> nums = new List<long>();
for (long i = 1; nums.Count < N; i++) {
// checking if the current number
// is of the given form
if (isValid(i)) {
// adding the number to the list
nums.Add(i);
}
}
return nums;
}
//Driver Code Starts
static void Main() {
int n = 15;
var ans = generateNumbers(n);
foreach (var x in ans) Console.Write(x + " ");
}
}
//Driver Code Ends
JavaScript
function isValid(curr) {
let n = curr;
// 2, 3, 5 and 7 should only be the
// prime factors of a valid number
while (n % 2 === 0) n = n / 2;
while (n % 3 === 0) n = n / 3;
while (n % 5 === 0) n = n / 5;
while (n % 7 === 0) n = n / 7;
return n === 1;
}
function generateNumbers(N) {
const nums = [];
for (let i = 1; nums.length < N; i++) {
// checking if the current number
// is of the given form
if (isValid(i)) {
// adding the number to the list
nums.push(i);
}
}
return nums;
}
//Driver Code Starts
// Driver code
const n = 15;
console.log(...generateNumbers(n));
//Driver Code Ends
Time complexity: O(X log X) - X is the last number among first N valid numbers of given form.
Space Complexity: O(1)
[Expected Approach] - Using Sorted Set - O(N log N) Time and O(N) Space
The main idea of this approach is to always pick the smallest number generated so far, add it to the final sequence, and then create new numbers by multiplying it with 2, 3, 5, and 7. Each newly formed number is kept for future processing, but only if it hasn’t been used before. By repeatedly taking the next smallest available number and expanding from it, the method gradually builds the sequence in strictly increasing order until the first N required numbers are collected.
C++
//Driver Code Starts
#include <iostream>
#include <set>
#include <vector>
using namespace std;
//Driver Code Ends
vector<long long> generateNumbers(int N) {
set<long long> nums;
nums.insert(1LL);
// to make sure that each integer is counted only once
set<long long> included;
// the list containing numbers in sorted order
vector<long long> res;
// generate numbers until we have N numbers
while ((int)res.size() < N) {
long long curr = *nums.begin();
nums.erase(nums.begin());
included.insert(curr);
res.push_back(curr);
// generate next candidates by multiplying with 2, 3, 5, and 7
if (!included.count(curr * 2)) nums.insert(curr * 2);
if (!included.count(curr * 3)) nums.insert(curr * 3);
if (!included.count(curr * 5)) nums.insert(curr * 5);
if (!included.count(curr * 7)) nums.insert(curr * 7);
}
return res;
}
//Driver Code Starts
int main() {
int n = 15;
vector<long long> nums = generateNumbers(n);
for (long long num : nums) {
cout << num << " ";
}
}
//Driver Code Ends
Java
//Driver Code Starts
import java.util.PriorityQueue;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.ArrayList;
class GFG {
//Driver Code Ends
static ArrayList<Long> generateNumbers(int N) {
TreeSet<Long> nums = new TreeSet<>();
nums.add(1L);
// to make sure that each integer is counted only once
HashSet<Long> included = new HashSet<>();
// the list containing numbers in sorted order
ArrayList<Long> res = new ArrayList<>();
// generate numbers until we have N numbers
while (res.size() < N ) {
long curr = nums.pollFirst();
included.add(curr);
res.add(curr);
// generate next candidates by multiplying with 2, 3, 5, and 7
if( !included.contains(curr*2)) nums.add(curr*2);
if( !included.contains(curr*3)) nums.add(curr*3);
if( !included.contains(curr*5)) nums.add(curr*5);
if( !included.contains(curr*7)) nums.add(curr*7);
}
return res;
}
//Driver Code Starts
public static void main(String[] args) {
int n = 15;
ArrayList<Long> nums = generateNumbers(n);
for( long num : nums)
System.out.print(num + " ");
}
}
//Driver Code Ends
Python
def generateNumbers(N):
nums = {1}
# to make sure that each integer is counted only once
included = set()
# the list containing numbers in sorted order
res = []
# generate numbers until we have N numbers
while len(res) < N:
curr = min(nums)
nums.remove(curr)
included.add(curr)
res.append(curr)
# generate next candidates by multiplying with 2, 3, 5, and 7
if curr * 2 not in included:
nums.add(curr * 2)
if curr * 3 not in included:
nums.add(curr * 3)
if curr * 5 not in included:
nums.add(curr * 5)
if curr * 7 not in included:
nums.add(curr * 7)
return res
#Driver Code Starts
if __name__ == "__main__":
n = 15
nums = generateNumbers(n)
print(*nums)
#Driver Code Ends
C#
//Driver Code Starts
using System;
using System.Collections.Generic;
class GFG {
//Driver Code Ends
static List<long> generateNumbers(int N) {
SortedSet<long> nums = new SortedSet<long>();
nums.Add(1L);
// to make sure that each integer is counted only once
HashSet<long> included = new HashSet<long>();
// the list containing numbers in sorted order
List<long> res = new List<long>();
// generate numbers until we have N numbers
while (res.Count < N) {
long curr = 0;
foreach (var x in nums) { curr = x; break; }
nums.Remove(curr);
included.Add(curr);
res.Add(curr);
// generate next candidates by multiplying with 2, 3, 5, and 7
if (!included.Contains(curr * 2)) nums.Add(curr * 2);
if (!included.Contains(curr * 3)) nums.Add(curr * 3);
if (!included.Contains(curr * 5)) nums.Add(curr * 5);
if (!included.Contains(curr * 7)) nums.Add(curr * 7);
}
return res;
}
//Driver Code Starts
public static void Main(string[] args) {
int n = 15;
List<long> nums = generateNumbers(n);
foreach (long num in nums)
Console.Write(num + " ");
}
}
//Driver Code Ends
JavaScript
function generateNumbers(N) {
let nums = new Set();
nums.add(1);
// to make sure that each integer is counted only once
let included = new Set();
// the list containing numbers in sorted order
let res = [];
// generate numbers until we have N numbers
while (res.length < N) {
let curr = Math.min(...nums);
nums.delete(curr);
included.add(curr);
res.push(curr);
// generate next candidates by multiplying with 2, 3, 5, and 7
if (!included.has(curr * 2)) nums.add(curr * 2);
if (!included.has(curr * 3)) nums.add(curr * 3);
if (!included.has(curr * 5)) nums.add(curr * 5);
if (!included.has(curr * 7)) nums.add(curr * 7);
}
return res;
}
//Driver Code Starts
// Driver code
let n = 15;
let nums = generateNumbers(n);
console.log(nums.join(" "));
//Driver Code Ends
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem