- Related Questions & Answers
- Minimum number of single digit primes required whose sum is equal to N in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Maximum sum subarray having sum less than or equal to given sums in C++
- C++ find four factors of N with maximum product and sum equal to N .
- Find four factors of N with maximum product and sum equal to N in C++
- Find N distinct numbers whose bitwise Or is equal to K in C++
- Count all triplets whose sum is equal to a perfect cube in C++
- Minimum number of squares whose sum equals to given number n
- How do you find continuous sub array whose sum is equal to a given number in Java?
- Find four factors of N with maximum product and sum equal to N - Set-2 in C++
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Find N distinct numbers whose bitwise Or is equal to K in Python
- Find the largest area rectangular sub-matrix whose sum is equal to k in C++
- Alternate Primes till N in C++?
- Maximum circular subarray sum in C++\n

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given a number n. Our task is to find the maximum count of primes whose sum is equal to given N.

Here, we will find the maximum number of prime numbers that when added will be equal to the number.

The prime number are those number which can be divide by either themselves or one.

let's take an example to understand the problem −

**Input** − N = 9

**Output** − 4

**Explanation** −

9 can be repressed as the sum of prime numbers in the following ways: 2, 2, 2, 3 3, 3, 3 2, 2, 5 2, 7 Out of these the maximum number of primes used is 4.

The maximum number of prime numbers that are used will be based on how many smallest prime numbers can be added to make the sum.

So, the smallest prime number is 2. And the successive greater prime number is 3, which is odd.

So, the count will be maxed if we use only 2’s and 3’s while calculating the sum. Based on this we can divide the problem, in two cases −

**Case 1** − if N is even, all prime numbers in the sum will be 2’s. So, the count will be n/2.

**Case 2** − if N is odd, all prime numbers in the sum will be 2’s except one which will be 3. So, the count will be (n-1/2).

**Program to find Maximum Primes whose sum is equal to given N in C++**

#include <iostream> using namespace std; int maxPrimeCount(int n){ //For odd case the result will same as (n-1)/2 return n / 2; } int main(){ int n = 9; cout<<"The maximum number of primes whose sum is equal to "<<n<<" is "<<maxPrimeCount(n); return 0; }

The maximum number of primes whose sum is equal to 9 is 4

Advertisements