Given two integers **N** and **M**, partition N into M integers such that the difference between the maximum and minimum integer obtained by the partition is as small as possible.

Print the M numbers **A1, A2….Am**, such that:

- sum(A) = N.
- max(A)-min(A) is minimised.

**Examples**:

Input: N = 11, M = 3Output: A[] = {4, 4, 3}Input: N = 8, M = 4Output: A[] = {2, 2, 2, 2}

To minimize the difference between the terms, we should have all of them as close to each other as possible. Let’s say, we could print any floating values instead of integers, the answer in that case would be 0 (print N/M M times). But since we need to print integers, we can divide it into 2 parts, floor(N/M) and floor(N/M)+1 which would give us the answer at most 1.

How many terms do we need to print of each type?

Let’s say we print **floor(N/M)** M times, the sum would be equal to **N – (N%M)**. So we need to choose **N%M** terms and increase it by 1.

Below is the implementation of the above approach:

## C++

`// C++ program to partition N into M parts ` `// such that difference Max and Min ` `// part is smallest ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to partition N into M parts such ` `// that difference Max and Min part ` `// is smallest ` `void` `printPartition(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `k = n / m; ` `// Minimum value ` ` ` ` ` `int` `ct = n % m; ` `// Number of (K+1) terms ` ` ` ` ` `int` `i; ` ` ` ` ` `for` `(i = 1; i <= ct; i++) ` ` ` `cout << k + 1 << ` `" "` `; ` ` ` ` ` `for` `(; i <= m; i++) ` ` ` `cout << k << ` `" "` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5, m = 2; ` ` ` ` ` `printPartition(n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to partition N into M parts ` `// such that difference Max and Min ` `// part is smallest ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to partition N into M parts such ` `// that difference Max and Min part ` `// is smallest ` `static` `void` `printPartition(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `k = n / m; ` `// Minimum value ` ` ` ` ` `int` `ct = n % m; ` `// Number of (K+1) terms ` ` ` ` ` `int` `i; ` ` ` ` ` `for` `(i = ` `1` `; i <= ct; i++) ` ` ` `System.out.print( k + ` `1` `+ ` `" "` `); ` ` ` ` ` `for` `(; i <= m; i++) ` ` ` `System.out.print( k + ` `" "` `); ` `} ` ` ` `// Driver Code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `5` `, m = ` `2` `; ` ` ` ` ` `printPartition(n, m); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to partition N into M parts ` `# such that difference Max and Min ` `# part is the smallest ` ` ` `# Function to partition N into M parts such ` `# that difference Max and Min part ` `# is smallest ` `def` `printPartition(n, m): ` ` ` `k ` `=` `int` `(n ` `/` `m) ` ` ` `# Minimum value ` ` ` ` ` `ct ` `=` `n ` `%` `m ` ` ` `# Number of (K+1) terms ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `,ct` `+` `1` `,` `1` `): ` ` ` `print` `(k ` `+` `1` `,end` `=` `" "` `) ` ` ` `count ` `=` `i ` ` ` `for` `i ` `in` `range` `(count,m,` `1` `): ` ` ` `print` `(k,end` `=` `" "` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `5` ` ` `m ` `=` `2` ` ` `printPartition(n, m) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to partition N into M parts ` `// such that difference Max and Min ` `// part is smallest ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `void` `printPartition(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `k = n / m; ` `// Minimum value ` ` ` ` ` `int` `ct = n % m; ` `// Number of (K+1) terms ` ` ` ` ` `int` `i; ` ` ` ` ` `for` `(i = 1; i <= ct; i++) ` ` ` `Console.Write( k + 1 + ` `" "` `); ` ` ` ` ` `for` `(; i <= m; i++) ` ` ` `Console.Write( k + ` `" "` `); ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `n = 5, m = 2; ` ` ` ` ` `printPartition(n, m); ` `} ` `} ` ` ` `// This code is contributed by Sachin ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to partition N into ` `// M parts such that difference ` `// Max and Min part is smallest ` ` ` `// Function to partition N into M ` `// parts such that difference Max ` `// and Min part is smallest ` `function` `printPartition(` `$n` `, ` `$m` `) ` `{ ` ` ` `$k` `= (int)(` `$n` `/ ` `$m` `); ` `// Minimum value ` ` ` ` ` `$ct` `= ` `$n` `% ` `$m` `; ` `// Number of (K+1) terms ` ` ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$ct` `; ` `$i` `++) ` ` ` `echo` `$k` `+ 1 . ` `" "` `; ` ` ` ` ` `for` `(; ` `$i` `<= ` `$m` `; ` `$i` `++) ` ` ` `echo` `$k` `. ` `" "` `; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `$m` `= 2; ` ` ` `printPartition(` `$n` `, ` `$m` `); ` ` ` `// This code is conributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3 2

**Time Complexity:** O(M)

## Recommended Posts:

- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- 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
- Divide N into K unique parts such that gcd of those parts is maximum
- Split the string into minimum parts such that each part is in the another string
- Partition a number into two divisible parts
- Split a string in equal parts such that all parts are palindromes
- Count ways to partition a string such that both parts have equal distinct characters
- Longest subarray such that difference of max and min is at-most K
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Remove minimum elements from the array such that 2*min becomes more than max
- Find the number of ways to divide number into four parts such that a = c and b = d
- Min and max length subarray having adjacent element difference atmost K
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Divide a number into two parts such that sum of digits is maximum
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Partition the array into two odd length groups with minimized absolute difference between their median
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Number of odd and even results for every value of x in range [min, max] after performing N steps

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.