Maximum Primes whose sum is equal to given N Last Updated : 21 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given a positive integer N > 1. Find the maximum count of prime numbers whose sum is equal to the given N.Examples: Input : N = 5 Output : 2 Explanation : 2 and 3 are two prime numbers whose sum is 5.Input : N = 6 Output :3 Explanation : 2, 2, 2 are three prime numbers whose sum is 6. For the maximum number of primes whose sum is equal to given n, prime numbers must be as small as possible. So, 2 is the smallest possible prime number and is an even number. The next prime number is greater than 2 in 3 which is odd. So, for any given n there are two conditions, either n will be odd or even. Case 1: n is even, In this case, n/2 will be the answer (n/2 number of 2 will result in the sum of n). Case 2: n is odd, In this case, floor(n/2) will be the answer ((n-3)/2 number of 2, and one 3 will result in the sum of n). Below is the implementation of the above approach: C++ // C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to find max count of primes int maxPrimes(int n) { // if n is even n/2 is required answer // if n is odd floor(n/2) = (int)(n/2) is required answer return n / 2; } // Driver Code int main() { int n = 17; cout << maxPrimes(n); return 0; } Java // Java program for above approach class GFG { // Function to find max count of primes static int maxPrimes(int n) { // if n is even n/2 is required answer // if n is odd floor(n/2) = (int)(n/2) // is required answer return n / 2; } // Driver Code public static void main(String[] args) { int n = 17; System.out.println(maxPrimes(n)); } } // This code is contributed // by Code_Mech Python3 # Python3 program for above approach # Function to find max count of primes def maxPrmimes(n): # if n is even n/2 is required answer # if n is odd floor(n/2) = (int)(n/2) # is required answer return n // 2 # Driver code n = 17 print(maxPrmimes(n)) # This code is contributed # by Shrikant13 C# // C# program for above approach using System; class GFG { // Function to find max count of primes static int maxPrimes(int n) { // if n is even n/2 is required answer // if n is odd floor(n/2) = (int)(n/2) // is required answer return n / 2; } // Driver Code public static void Main() { int n = 17; Console.WriteLine(maxPrimes(n)); } } // This code is contributed // by Akanksha Rai PHP <?php // PHP program for above approach // Function to find max count of primes function maxPrimes($n) { // if n is even n/2 is required answer // if n is odd floor(n/2) = (int)(n/2) is required answer return (int)($n / 2); } // Driver Code $n = 17; echo maxPrimes($n); // This code is contributed by mits ?> JavaScript <script> // Javascript program for above approach // Function to find max count of primes function maxPrimes(n) { // if n is even n/2 is required answer // if n is odd floor(n/2) = (int)(n/2) is required answer return parseInt(n / 2); } // Driver Code var n = 17; document.write( maxPrimes(n)); </script> Output: 8 Time Complexity: O(1)Auxiliary Space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Maximum Primes whose sum is equal to given N S Shivam.Pradhan Follow Improve Article Tags : Mathematical DSA Prime Number Practice Tags : MathematicalPrime Number Similar Reads Maximum sum subarray having sum less than or equal to given sum You are given an array of non-negative integers and a target sum. Your task is to find a contiguous subarray whose sum is the maximum possible, while ensuring that it does not exceed the given target sum.Note: The given array contains only non-negative integers.Examples: Input: arr[] = [1, 2, 3, 4, 6 min read Maximum Sum of Array with given MEX Given 3 integers N, K, and X, the task is to construct an array arr[] with the below conditions: Size of the array = NMEX of the array = KAll array elements should be at most XAmong all the array that follows the above condition print the one having the maximum sum of its elements or print -1 if no 7 min read Maximum size subset with given sum This is an extended version of the subset sum problem. Here we need to find the size of the maximum size subset whose sum is equal to the given sum. Examples: Input : set[] = {2, 3, 5, 7, 10, 15}, sum = 10 Output : 3 The largest sized subset with sum 10 is {2, 3, 5} Input : set[] = {1, 2, 3, 4, 5} s 8 min read Maximum Count of pairs having equal Sum based on the given conditions Given an array arr[] of length N containing array elements in the range [1, N], the task is to find the maximum number of pairs having equal sum, given that any element from the array can only be part of a single pair. Examples: Input: arr[] = {1, 4, 1, 4} Output: 2 Explanation: Pairs {{1, 4}, {1, 4 8 min read Maximum number made up of distinct digits whose sum is equal to N Given a positive integer N, the task is to find the largest positive number made up of distinct digits having the sum of its digits equal to N. If no such number exists, print â-1â. Examples: Input: N = 25Output: 98710Explanation:The number 98710 is the largest number that contains only unique digit 7 min read Maximum sum with limited queries Given an array arr[] of n integers in sorted order, find the maximum number of largest elements that can be selected from the array such that the sum is less than or equal to k. You can select an element any number of times. The sum of the selected elements should be maximized. You can query the val 7 min read Find maximum sum array of length less than or equal to m Given n arrays of different lengths consisting of integers, the target is to pick atmost one subarray from an array such that the combined length of all picked sub arrays does not become greater than m and also sum of their elements is maximum.(also given that value of n can not be more than 100) Pr 13 min read Maximum sum such that no two are adjacent Given an array of positive numbers, find the maximum sum of a subsequence such that no two numbers in the subsequence should be adjacent in the array. Examples: Input: arr[] = {5, 5, 10, 100, 10, 5}Output: 110Explanation: Pick the subsequence {5, 100, 5}.The sum is 110 and no two elements are adjace 15+ min read Recursively break a number in 3 parts to get maximum sum Given a number n, we can divide it in only three parts n/2, n/3 and n/4 (we will consider only integer part). The task is to find the maximum sum we can make by dividing number in three parts recursively and summing up them together.Examples: Input : n = 12 Output : 13 // We break n = 12 in three pa 12 min read Biggest integer which has maximum digit sum in range from 1 to n Given a number n, find a number in range from 1 to n such that its sum is maximum. If there are several such integers, determine the biggest of them. Examples : Input: n = 100 Output: 99 99 is the largest number in range from 1 to 100 with maximum sum of digits. Input: n = 48 Output: 48 Explanation: 8 min read Like