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.
Input: N = 52, K = 5
Output: 6 10 14 15 7
N = 52 can be expressed as 6 10 14 15 2, where 15 = 3 * 5, 14 = 2*7, 10 = 2*5, 6 = 2*3, i.e, atleast 4 numbers are product of 2 distinct prime numbers.
Input: N = 44 K = 5
Explanation: It is not possible to express N as product of distinct numbers.
Approach: Follow the steps below to solve the problem:
- Store all prime numbers in a vector using Sieve of Eratosthenes.
- Iterate through the prime numbers stored and store the product of every pair of a prime number in another vector prod.
- Print the first K – 1 elements of prod vector
- If the sum of the first K – 1 elements of prod vector is more than N then print -1.
Below is the implementation of the above approach:
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.
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Count possible permutations of given array satisfying the given conditions
- Count valid pairs in the array satisfying given conditions
- Generate N integers satisfying the given conditions
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Split the array into equal sum parts according to given conditions
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Maximum possible sum of squares of stack elements satisfying the given properties
- Sum of minimum value of x and y satisfying the equation ax + by = c
- Check if elements of an array can be arranged satisfying the given condition
- Count of pairs satisfying the given condition
- Smallest positive integer X satisfying the given equation
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Find numbers a and b that satisfy the given conditions
- Count of N digit numbers possible which satisfy the given conditions
- Find x and y satisfying ax + by = n
- Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N
- Find the Maximum possible Sum for the given conditions
- Find maximum Subsequence Sum according to given conditions
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.