Given an integer N( 2 <= N <= 10^9 ), split the number into one or more parts(possibly none), where each part must be greater than 1. The task is to find the minimum possible sum of the second largest divisor of all the splitting numbers.
Input : N = 27 Output : 3 Explanation : Split the given number into 19, 5, 3. Second largest divisor of each number is 1. So, sum is 3. Input : N = 19 Output : 1 Explanation : Don't make any splits. Second largest divisor of 19 is 1. So, sum is 1
The idea is based on Goldbach’s conjecture.
- When the number is prime, then the answer will be 1.
- When a number is even then it can always be expressed as a sum of 2 primes. So, the answer will be 2.
- When the number is odd,
- When N-2 is prime, then the number can be express as the sum of 2 primes, that are 2 and N-2, then the answer will be 2.
- Otherwise, the answer will always be 3.
Below is the implementation of the above approach:
Time complexity: O(sqrt(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.