Split the number into N parts such that difference between the smallest and the largest part is minimum | 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
< ? php function split ( $ x , $ n ) { if ( $ x < $ n ) echo ( -1 ) ; else if ( $ x % $ n == 0 ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo ( $ x / $ n ) ; echo ( " ▁ " ) ; } } else { $ 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 ( " ▁ " ) ; } } } } $ x = 5 ; $ n = 3 ; split ( $ x , $ n ) ; ? >