Split N as the sum of K numbers satisfying the given conditions

Split N as the sum of K numbers satisfying the given conditions

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:

  • 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:

  • 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.

arrays mathematical sorting prime number sieve

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Nearest prime number in the array of every array element

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.

Sum of prime numbers in range [L, R] from given Array for Q queries

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.

Count of numbers upto M divisible by given Prime Numbers - GeeksforGeeks

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.

Rearrange Array to maximize number having Array elements

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.

Length of longest subarray having only K distinct Prime Numbers

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.