Given two numbers **N** and **K**, the task is to split this number into **K** positive integers such that their sum is equal to **N** and none of these **K** integers is a multiple of K.

**Note:** *N>=2*

**Examples:**

Input:N = 10, K = 3

Output:1, 1, 8

Input:N = 18, K = 3

Output:1, 1, 16

**Approach:**

To split **N** into **K** numbers, we need to approach the problem by the following steps:

- Check if
**N**is divisible by**K**or not. - If
**N**is divisible by**K**, then**N – K + 1**is not divisible by**K**. Hence, we can split N into**N – K + 1**as one part and all the remaining**K – 1**parts as**1**. - If
**N**is not divisible by**K**:- If
**K**is 2, then it is not possible to split. - Otherwise, split
**N**into**K-2**parts as all**1**and**2**and**N – K**as the remaining two parts.

- If

Below code is the implementation of the above approach:

## C++

`// C++ program to split a number ` `// as sum of K numbers which are ` `// not divisible by K ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to split into K parts ` `// and print them ` `void` `printKParts(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(N % K == 0) { ` ` ` ` ` `// Print 1 K - 1 times ` ` ` `for` `(` `int` `i = 1; i < K; i++) ` ` ` `cout << ` `"1, "` `; ` ` ` ` ` `// Print N - K + 1 ` ` ` `cout << N - (K - 1) << endl; ` ` ` `} ` ` ` `else` `{ ` ` ` `if` `(K == 2) { ` ` ` `cout << ` `"Not Possible"` `<< endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print 1 K-2 times ` ` ` `for` `(` `int` `i = 1; i < K - 1; i++) ` ` ` `cout << 1 << ` `", "` `; ` ` ` ` ` `// Print 2 and N - K ` ` ` `cout << 2 << ` `", "` ` ` `<< N - K << endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 18, K = 5; ` ` ` `printKParts(N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to split a number ` `// as sum of K numbers which are ` `// not divisible by K ` `class` `GFG{ ` ` ` `// Function to split into K parts ` `// and print them ` `static` `void` `printKParts(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(N % K == ` `0` `) ` ` ` `{ ` ` ` ` ` `// Print 1 K - 1 times ` ` ` `for` `(` `int` `i = ` `1` `; i < K; i++) ` ` ` `System.out.print(` `"1, "` `); ` ` ` ` ` `// Print N - K + 1 ` ` ` `System.out.print(N - (K - ` `1` `) + ` `"\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `if` `(K == ` `2` `) ` ` ` `{ ` ` ` `System.out.print(` `"Not Possible"` `+ ` `"\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print 1 K-2 times ` ` ` `for` `(` `int` `i = ` `1` `; i < K - ` `1` `; i++) ` ` ` `System.out.print(` `1` `+ ` `", "` `); ` ` ` ` ` `// Print 2 and N - K ` ` ` `System.out.print(` `2` `+ ` `", "` `+ (N - K) + ` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `18` `, K = ` `5` `; ` ` ` ` ` `printKParts(N, K); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to split a number ` `# as sum of K numbers which are ` `# not divisible by K ` ` ` `# Function to split into K parts ` `# and print them ` `def` `printKParts(N, K): ` ` ` ` ` `if` `(N ` `%` `K ` `=` `=` `0` `): ` ` ` ` ` `# Print 1 K - 1 times ` ` ` `for` `i ` `in` `range` `(` `1` `, K): ` ` ` `print` `(` `"1, "` `); ` ` ` ` ` `# Print N - K + 1 ` ` ` `print` `(N ` `-` `(K ` `-` `1` `), end ` `=` `""); ` ` ` `else` `: ` ` ` `if` `(K ` `=` `=` `2` `): ` ` ` `print` `(` `"Not Possible"` `, end ` `=` `""); ` ` ` `return` `; ` ` ` ` ` `# Print 1 K-2 times ` ` ` `for` `i ` `in` `range` `(` `1` `, K ` `-` `1` `): ` ` ` `print` `(` `1` `, end ` `=` `", "` `); ` ` ` ` ` `# Print 2 and N - K ` ` ` `print` `(` `2` `, ` `", "` `, (N ` `-` `K), end ` `=` `""); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `18` `; ` ` ` `K ` `=` `5` `; ` ` ` ` ` `printKParts(N, K); ` ` ` `# This code is contributed by Rohit_ranjan ` |

*chevron_right*

*filter_none*

## C#

`// C# program to split a number ` `// as sum of K numbers which are ` `// not divisible by K ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to split into K parts ` `// and print them ` `static` `void` `printKParts(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(N % K == 0) ` ` ` `{ ` ` ` ` ` `// Print 1 K - 1 times ` ` ` `for` `(` `int` `i = 1; i < K; i++) ` ` ` `Console.Write(` `"1, "` `); ` ` ` ` ` `// Print N - K + 1 ` ` ` `Console.Write(N - (K - 1) + ` `"\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `if` `(K == 2) ` ` ` `{ ` ` ` `Console.Write(` `"Not Possible"` `+ ` `"\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print 1 K-2 times ` ` ` `for` `(` `int` `i = 1; i < K - 1; i++) ` ` ` `Console.Write(1 + ` `", "` `); ` ` ` ` ` `// Print 2 and N - K ` ` ` `Console.Write(2 + ` `", "` `+ (N - K) + ` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 18, K = 5; ` ` ` ` ` `printKParts(N, K); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

1, 1, 1, 2, 13

**Time Complexity:** *O(K)*

## Recommended Posts:

- Find permutation of n which is divisible by 3 but not divisible by 6
- Sum of first K numbers which are not divisible by N
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Number of ways to split a binary number such that every part is divisible by 2
- Find the Nth natural number which is not divisible by A
- Find the Kth number which is not divisible by N
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Count numbers which are divisible by all the numbers from 2 to 10
- Split the number N by maximizing the count of subparts divisible by K
- Split a number into 3 parts such that none of the parts is divisible by 3
- Sum of numbers from 1 to N which are divisible by 3 or 4
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum and Product of all Composite numbers which are divisible by k in an array
- Sum of first N natural numbers which are divisible by X or Y
- Sum of all numbers in the given range which are divisible by M
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Numbers that are not divisible by any number in the range [2, 10]
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Smallest composite number not divisible by first N prime numbers

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.