Split the number into N parts such that difference between the smallest and the largest part is minimum

Given two integers ‘X’ and ‘N’, the task is to split the integer ‘X’ into exactly ‘N’ parts such that:
X1 + X2 + X3 + ... + Xn = X and the difference between the maximum and the minimum number from the sequence is minimized.
Print the sequence in the end, if the number cannot be divided into exactly ‘N’ parts then print ‘-1’ instead.

Examples:

Input: X = 5, N = 3
Output: 1 2 2
Divide 5 into 3 parts such that the difference between the largest and smallest integer among
them is as minimal as possible. So we divide 5 as 1 + 2 + 2.



Input: X = 25, N = 5
Output: 5 5 5 5 5

Approach: There is always a way of splitting the number if X >= N.

  • If the number is being split into exactly ‘N’ parts then every part will have the value X/N and the remaining X%N part can be distributed among any X%N numbers.
  • Thus, if X % N == 0 then the minimum difference will always be ‘0’ and the sequence will contain all equal numbers i.e. x/n.
  • Else, the difference will be ‘1’ and the sequence will be X/N, X/N, ..., (X/N)+1, (X/N)+1..

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation of the approach
#include<bits/stdc++.h>
using namespace std;;
  
// Function that prints 
// the required sequence
void split(int x, int n)
{
  
// If we cannot split the 
// number into exactly 'N' parts
if(x < n)
cout<<"-1"<<" ";
  
          
  
    // If x % n == 0 then the minimum 
    // difference is 0 and all 
    // numbers are x / n
    else if (x % n == 0)
    {
        for(int i=0;i<n;i++)
        cout<<(x/n)<<" ";
    }
    else
    {
  
        // upto n-(x % n) the values 
        // will be x / n 
        // after that the values 
        // will be x / n + 1
        int zp = n - (x % n);
        int pp = x/n;
        for(int i=0;i<n;i++) 
        {
  
            if(i>= zp)
            cout<<(pp + 1)<<" ";
            else
            cout<<pp<<" ";
        }
    }
}
      
// Driver code
int main()
          
int x = 5;
int n = 3;
split(x, n);
  
}
//THis code is contributed 
// Surendra_Gangwar

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
   
class GFG{
// Function that prints 
// the required sequence
static void split(int x, int n)
{
   
// If we cannot split the 
// number into exactly 'N' parts
if(x < n)
System.out.print("-1 ");
   
           
   
    // If x % n == 0 then the minimum 
    // difference is 0 and all 
    // numbers are x / n
    else if (x % n == 0)
    {
        for(int i=0;i<n;i++)
        System.out.print((x/n)+" ");
    }
    else
    {
   
        // upto n-(x % n) the values 
        // will be x / n 
        // after that the values 
        // will be x / n + 1
        int zp = n - (x % n);
        int pp = x/n;
        for(int i=0;i<n;i++) 
        {
   
            if(i>= zp)
            System.out.print((pp + 1)+" ");
            else
            System.out.print(pp+" ");
        }
    }
}
       
// Driver code
public static void main(String[] args)
           
int x = 5;
int n = 3;
split(x, n);
   
}
}
//This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function that prints 
# the required sequence
def split(x, n):
  
    # If we cannot split the 
    # number into exactly 'N' parts
    if(x < n): 
        print(-1)
  
    # If x % n == 0 then the minimum 
    # difference is 0 and all 
    # numbers are x / n
    elif (x % n == 0):
        for i in range(n):
            print(x//n, end =" ")
    else:
        # upto n-(x % n) the values 
        # will be x / n 
        # after that the values 
        # will be x / n + 1
        zp = n - (x % n)
        pp = x//n
        for i in range(n):
            if(i>= zp):
                print(pp + 1, end =" ")
            else:
                print(pp, end =" ")
        
# Driver code          
x = 5
n = 3
split(x, n)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
public class GFG{
    // Function that prints 
// the required sequence 
static void split(int x, int n) 
  
// If we cannot split the 
// number into exactly 'N' parts 
if(x < n) 
Console.WriteLine("-1 "); 
  
          
  
    // If x % n == 0 then the minimum 
    // difference is 0 and all 
    // numbers are x / n 
    else if (x % n == 0) 
    
        for(int i=0;i<n;i++) 
    Console.Write((x/n)+" "); 
    
    else
    
  
        // upto n-(x % n) the values 
        // will be x / n 
        // after that the values 
        // will be x / n + 1 
        int zp = n - (x % n); 
        int pp = x/n; 
        for(int i=0;i<n;i++) 
        
  
            if(i>= zp) 
            Console.Write((pp + 1)+" "); 
            else
            Console.Write(pp+" "); 
        
    
      
// Driver code 
static public void Main (){
  
int x = 5; 
int n = 3; 
split(x, n); 
  
//This code is contributed by Sachin.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function that prints 
// the required sequence
function split($x, $n)
{
    // If we cannot split the 
    // number into exactly 'N' parts
    if($x < $n)
        echo (-1);
  
    // If x % n == 0 then the minimum 
    // difference is 0 and all 
    // numbers are x / n
    else if ($x % $n == 0)
    {
        for($i = 0; $i < $n; $i++)
        {
            echo ($x / $n);
            echo (" ");
        }
    }
      
    else
    {
        // upto n-(x % n) the values 
        // will be x / n 
        // after that the values 
        // will be x / n + 1
        $zp = $n - ($x % $n);
        $pp = $x / $n;
        for ($i = 0; $i < $n; $i++)
        {
            if($i >= $zp)
            {
                echo (int)$pp + 1;
                echo (" ");
            }
            else
            {
                echo (int)$pp;
                echo (" ");
            }
        }
    }
}
  
// Driver code     
$x = 5;
$n = 3;
split( $x, $n);
  
// This code is contributed 
// by Shivi_Aggarwal
?>

chevron_right


Output:

1 2 2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.