# Divide N into K parts in the form (X, 2X, … , KX) for some value of X

Given a positive integer **N** and **K**, the task is to divide **N** into **K** parts such that the first part has a value **X**, the second part is **2X**, and so on for some value of **X**. If such division is not possible then print **-1**.

**Examples:**

Input:N = 10, K = 4Output:1 2 3 4Explanation:

If we take 1 as first number second number will be 2 third number will be 3 times first which is 3 and the last number will be 4 times third number so last number is 4. We can note that sum=1+2+3+4=10 which is the required sum.

InputN = 10, K = 3Output:-1Explanation:

Distributing N in 3 parts with given constraint is not possible.

**Approach:** To solve the problem mentioned above let’s to understand it’s mathematical implementation. Let the division be X_{1}, X_{2}, X_{3} up to X_{K} where the second integer is X_{1} * 2, third one is X_{1} * 3 and the Kth one is X_{1} * K.

We know that,

=>

=>

=>

=> , where ( 1 + 2 + 3 + … + K) =

=>

So to solve the problem we have to follow the steps given below:

- Calculate the value of
**K * (K + 1)**and divide**2 * N**by**K * (K + 1)**in order to get value of X_{1}. - If X
_{1}is not an integer in the above step then print**-1**as there is no such division is possible. - To get the value of X
_{2}we will multiply X_{1}by 2. Similarly, to get X_{K}multiply X_{1}with**K**. - After finding all teh values print them.

Below is the implementation of above approach:

## C++

`// C++ program for the above approach` `#include <iostream>` `using` `namespace` `std;` `typedef` `long` `long` `int` `ll;` ` ` `// Function to find the division` `void` `solve(` `int` `n, ` `int` `k)` `{` ` ` `int` `x1, d;` ` ` ` ` `// Calculating value of x1` ` ` `d = k * (k + 1);` ` ` ` ` `// Print -1 if division` ` ` `// is not possible` ` ` `if` `((2 * n) % d != 0) {` ` ` `cout << ` `"-1"` `;` ` ` `return` `;` ` ` `}` ` ` ` ` `x1 = 2 * n / d;` ` ` ` ` `// Get the first number ie x1` ` ` `// then successively multiply` ` ` `// it by x1 k times by index number` ` ` `// to get the required answer` ` ` `for` `(` `int` `i = 1; i <= k; i++) {` ` ` `cout << x1 * i << ` `" "` `;` ` ` `}` ` ` `cout << endl;` `}` ` ` `// Driver Code` `int` `main()` `{` ` ` `// Given N and K` ` ` `int` `n = 10, k = 4;` ` ` ` ` `// Function Call` ` ` `solve(n, k);` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to find the division` `static` `void` `solve(` `int` `n, ` `int` `k)` `{` ` ` `int` `x1, d;` ` ` ` ` `// Calculating value of x1` ` ` `d = k * (k + ` `1` `);` ` ` ` ` `// Print -1 if division` ` ` `// is not possible` ` ` `if` `((` `2` `* n) % d != ` `0` `) ` ` ` `{` ` ` `System.out.print(` `"-1"` `);` ` ` `return` `;` ` ` `}` ` ` ` ` `x1 = ` `2` `* n / d;` ` ` ` ` `// Get the first number ie x1` ` ` `// then successively multiply` ` ` `// it by x1 k times by index number` ` ` `// to get the required answer` ` ` `for` `(` `int` `i = ` `1` `; i <= k; i++) ` ` ` `{` ` ` `System.out.print(x1 * i+ ` `" "` `);` ` ` `}` ` ` `System.out.println();` `}` ` ` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given N and K` ` ` `int` `n = ` `10` `, k = ` `4` `;` ` ` ` ` `// Function Call` ` ` `solve(n, k);` `}` `}` ` ` `// This code is contributed by 29AjayKumar` |

## Python3

`# Python3 program for the above approach` ` ` `# Function to find the division` `def` `solve(n, k):` ` ` ` ` `# Calculating value of x1` ` ` `d ` `=` `k ` `*` `(k ` `+` `1` `);` ` ` ` ` `# Print -1 if division` ` ` `# is not possible` ` ` `if` `((` `2` `*` `n) ` `%` `d !` `=` `0` `):` ` ` `print` `(` `"-1"` `);` ` ` `return` `;` ` ` ` ` ` ` `x1 ` `=` `2` `*` `n ` `/` `/` `d;` ` ` ` ` `# Get the first number ie x1` ` ` `# then successively multiply` ` ` `# it by x1 k times by index number` ` ` `# to get the required answer` ` ` `for` `i ` `in` `range` `(` `1` `, k ` `+` `1` `):` ` ` `print` `(x1 ` `*` `i, end ` `=` `" "` `);` ` ` `# Driver Code` ` ` `# Given N and K` `n ` `=` `10` `; k ` `=` `4` `;` ` ` `# Function Call` `solve(n, k);` ` ` `# This code is contributed by Code_Mech` |

## C#

`// Java program for the above approach` `import java.util.*;` `class` `GFG{` ` ` `// Function to find the division` `static` `void` `solve(` `int` `n, ` `int` `k)` `{` ` ` `int` `x1, d;` ` ` ` ` `// Calculating value of x1` ` ` `d = k * (k + 1);` ` ` ` ` `// Print -1 if division` ` ` `// is not possible` ` ` `if` `((2 * n) % d != 0) ` ` ` `{` ` ` `System.` `out` `.print(` `"-1"` `);` ` ` `return` `;` ` ` `}` ` ` ` ` `x1 = 2 * n / d;` ` ` ` ` `// Get the first number ie x1` ` ` `// then successively multiply` ` ` `// it by x1 k times by index number` ` ` `// to get the required answer` ` ` `for` `(` `int` `i = 1; i <= k; i++) ` ` ` `{` ` ` `System.` `out` `.print(x1 * i+ ` `" "` `);` ` ` `}` ` ` `System.` `out` `.println();` `}` ` ` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given N and K` ` ` `int` `n = 10, k = 4;` ` ` ` ` `// Function Call` ` ` `solve(n, k);` `}` `}` ` ` `// This code is contributed by 29AjayKumar` |

**Output:**

1 2 3 4

**Time Complexity:** *O(K)***Auxiliary Space:** *O(1)*

Attention reader! Don’t stop learning now. Participate in the **Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students**.