Given an integer N ≥ 2, you can split the number as a sum of k integers i.e. N = k1 + k2 + … + kn where each kth element is ≥ 2 then the cost of splitting is calculated as maxDiv(k1) + maxDiv(k2) + … + maxDiv(kn) where maxDiv(x) is the maximum divisor of x which is < x.
The task is to split the number in such a way that the cost is minimized, print the minimized cost in the end.
Input: N = 6
6 can be represented as (3 + 3) and the cost will be 1 + 1 = 2.
Input: N = 5
- When n is prime then the cost will be 1 as we don’t have to split n and the greatest divisor of n less than itself will be 1.
- If n is odd and n – 2 is prime then n can be split into (2 + prime) which will cost 1 + 1 = 2.
- If n is even then the cost will be 2 as according to Goldbach’s conjecture, every even number greater than 2 can be expressed as sum of two primes which is proven till 4 * 1018.
- If all of the above conditions are not satisfied then n must be odd now and if 3 is subtracted from n then it will become even which can be expressed as (3 + even) = (3 + prime + prime) which will cost 3.
Below is the implementation of the above approach:
- Minimize the cost of buying the Objects
- Minimize the cost of partitioning an array into K groups
- Minimize the cost of selecting two numbers whose product is X
- Minimize cost to convert given two integers to zero using given operations
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Minimize cost to Swap two given Arrays
- Minimize cost to empty a given string by removing characters alphabetically
- Minimize cost to convert a given matrix to another by flipping columns and reordering rows
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Number of ways to split a binary number such that every part is divisible by 2
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimize the number by changing at most K digits
- Minimize number of cuts required to break N length stick into N unit length sticks
- Minimize number of boxes by putting small box inside bigger one
- Prime points (Points that split a number into two primes)
- Split the number N by maximizing the count of subparts divisible by K
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Split a number into 3 parts such that none of the parts is divisible by 3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.