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