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.

**Examples:**

Input :N = 27Output :3Explanation :Split the given number into 19, 5, 3. Second largest divisor of each number is 1. So, sum is 3.Input :N = 19Output :1Explanation :Don't make any splits. Second largest divisor of 19 is 1. So, sum is 1

**Approach: **

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:

## C++

`// CPP program to find sum of all second largest divisor ` `// after splitting a number into one or more parts ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find a number is prime or not ` `bool` `prime(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `false` `; ` ` ` ` ` `// If there is any divisor ` ` ` `for` `(` `int` `i = 2; i * i <= n; ++i) ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function to find the sum of all second largest divisor ` `// after splitting a number into one or more parts ` `int` `Min_Sum(` `int` `n) ` `{ ` ` ` `// If number is prime ` ` ` `if` `(prime(n)) ` ` ` `return` `1; ` ` ` ` ` `// If n is even ` ` ` `if` `(n % 2 == 0) ` ` ` `return` `2; ` ` ` ` ` `// If the number is odd ` ` ` `else` `{ ` ` ` ` ` `// If N-2 is prime ` ` ` `if` `(prime(n - 2)) ` ` ` `return` `2; ` ` ` ` ` `// There exists 3 primes x1, x2, x3 ` ` ` `// such that x1 + x2 + x3 = n ` ` ` `else` ` ` `return` `3; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 27; ` ` ` ` ` `// Function call ` ` ` `cout << Min_Sum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Sum of all second largest ` `// divisors after splitting a number into one or more parts ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` ` ` `// Function to find a number is prime or not ` `static` `boolean` `prime(` `int` `n) ` `{ ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `false` `; ` ` ` ` ` `// If there is any divisor ` ` ` `for` `(` `int` `i = ` `2` `; i * i <= n; ++i) ` ` ` `if` `(n % i == ` `0` `) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function to find the sum of all second largest divisor ` `// after splitting a number into one or more parts ` `static` `int` `Min_Sum(` `int` `n) ` `{ ` ` ` `// If number is prime ` ` ` `if` `(prime(n)) ` ` ` `return` `1` `; ` ` ` ` ` `// If n is even ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `return` `2` `; ` ` ` ` ` `// If the number is odd ` ` ` `else` `{ ` ` ` ` ` `// If N-2 is prime ` ` ` `if` `(prime(n - ` `2` `)) ` ` ` `return` `2` `; ` ` ` ` ` `// There exists 3 primes x1, x2, x3 ` ` ` `// such that x1 + x2 + x3 = n ` ` ` `else` ` ` `return` `3` `; ` ` ` `} ` `} ` ` ` `// Driver code ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `27` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println( Min_Sum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_6 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find sum of all second largest divisor ` `# after splitting a number into one or more parts ` ` ` `from` `math ` `import` `sqrt ` `# Function to find a number is prime or not ` `def` `prime(n): ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `False` ` ` ` ` `# If there is any divisor ` ` ` `for` `i ` `in` `range` `(` `2` `,` `int` `(sqrt(n))` `+` `1` `,` `1` `): ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Function to find the sum of all second largest divisor ` `# after splitting a number into one or more parts ` `def` `Min_Sum(n): ` ` ` `# If number is prime ` ` ` `if` `(prime(n)): ` ` ` `return` `1` ` ` ` ` `# If n is even ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `return` `2` ` ` ` ` `# If the number is odd ` ` ` `else` `: ` ` ` `# If N-2 is prime ` ` ` `if` `(prime(n ` `-` `2` `)): ` ` ` `return` `2` ` ` ` ` `# There exists 3 primes x1, x2, x3 ` ` ` `# such that x1 + x2 + x3 = n ` ` ` `else` `: ` ` ` `return` `3` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `27` ` ` ` ` `# Function call ` ` ` `print` `(Min_Sum(n)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to Sum of all second largest ` `// divisors after splitting a number into one or more parts ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find a number is prime or not ` `static` `bool` `prime(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `false` `; ` ` ` ` ` `// If there is any divisor ` ` ` `for` `(` `int` `i = 2; i * i <= n; ++i) ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function to find the sum of all second largest divisor ` `// after splitting a number into one or more parts ` `static` `int` `Min_Sum(` `int` `n) ` `{ ` ` ` `// If number is prime ` ` ` `if` `(prime(n)) ` ` ` `return` `1; ` ` ` ` ` `// If n is even ` ` ` `if` `(n % 2 == 0) ` ` ` `return` `2; ` ` ` ` ` `// If the number is odd ` ` ` `else` `{ ` ` ` ` ` `// If N-2 is prime ` ` ` `if` `(prime(n - 2)) ` ` ` `return` `2; ` ` ` ` ` `// There exists 3 primes x1, x2, x3 ` ` ` `// such that x1 + x2 + x3 = n ` ` ` `else` ` ` `return` `3; ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `n = 27; ` ` ` ` ` `// Function call ` ` ` `Console.WriteLine( Min_Sum(n)); ` `} ` `} ` ` ` `// This code is contributed by anuj_6 ` |

*chevron_right*

*filter_none*

**Output:**

3

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

## Recommended Posts:

- Split a number into 3 parts such that none of the parts is divisible by 3
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Divide N into K unique parts such that gcd of those parts is maximum
- Break a number such that sum of maximum divisors of all parts is minimum
- Find sum of divisors of all the divisors of a natural number
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Split array into K subsets to maximize sum of their second largest elements
- Count number of rotated strings which have more number of vowels in the first half than second half
- Divide a number into two parts such that sum of digits is maximum
- Find the number of ways to divide number into four parts such that a = c and b = d
- Check if a number has an odd count of odd divisors and even count of even divisors
- Find largest sum of digits in all divisors of n
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Count of divisors having more set bits than quotient on dividing N
- Partition a number into two divisible parts
- Divide number into two parts divisible by given numbers
- Break the number into three parts
- Divide a big number into two parts that differ by k
- Divide a number into two parts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.