A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Given an integer N, the task is to express the given number as the sum of K numbers where at least K – 1 numbers are distinct and are product of 2 primes. If no possible answer, exists, print -1.
Examples:
Input:_ N = 52, K = 5_
Output:_ 6 10 14 15 7_
Explanation:
N = 52 can be expressed as 6 10 14 15 2, where 15 = 3 * 5, 14 = 27, 10 = 25, 6 = 2*3, i.e, atleast 4 numbers are product of 2 distinct prime numbers.
Input:_ N = 44 K = 5_
Output:_ -1_
Explanation:_ It is not possible to express N as product of distinct numbers._
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
filter_none
edit
play_arrow
brightness_4
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Vector to store prime numbers
vector<``**int**``> primes;
// Function to generate prime
// numbers using SieveOfEratosthenes
**void**
SieveOfEratosthenes()
{
// Boolean array to store primes
**bool**
prime[10005];
**memset**``(prime,
**true**``,
**sizeof**``(prime));
**for**
(``**int**
p = 2; p * p <= 1000; p++) {
// If p is a prime
**if**
(prime[p] ==
**true**``) {
// Mark all its multiples as non-prime
**for**
(``**int**
i = p * p; i <= 1000; i += p)
prime[i] =
**false**``;
}
}
// Print all prime numbers
**for**
(``**int**
p = 2; p <= 1000; p++)
**if**
(prime[p])
primes.push_back(p);
}
// Function to generate n as the sum
// of k numbers where atleast K - 1
// are distinct and are product of 2 primes
**void**
generate(``**int**
n,
**int**
k)
{
// Stores the product of every
// pair of prime number
vector<``**long**
**long**``> prod;
SieveOfEratosthenes();
**int**
l = primes.size();
**for**
(``**int**
i = 0; i < l; i++) {
**for**
(``**int**
j = i + 1; j < l; j++) {
**if**
(primes[i] * primes[j] > 0)
prod.push_back(primes[i]
* primes[j]);
}
}
// Sort the products
sort(prod.begin(), prod.end());
**int**
sum = 0;
**for**
(``**int**
i = 0; i < k - 1; i++)
sum += prod[i];
// If sum exceeds n
**if**
(sum > n)
cout <<
"-1"``;
// Otherwise, print the k
// required numbers
**else**
{
**for**
(``**int**
i = 0; i < k - 1; i++) {
cout << prod[i] <<
", "``;
}
cout << n - sum <<
"\n"``;
}
}
// Driver Code
**int**
main()
{
**int**
n = 52, k = 5;
generate(n, k);
**return**
0;
}
Output:
6, 10, 14, 15, 7
Time complexity:_ O(N log N)_
Auxiliary Space:_ O(N)_
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.