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.
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/Nand the remaining
X%Npart can be distributed among any
- Thus, if
X % N == 0then the minimum difference will always be ‘0’ and the sequence will contain all equal numbers i.e.
- 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:
1 2 2
- Partition N into M parts such that difference between Max and Min part is smallest
- Split a number into 3 parts such that none of the parts is divisible by 3
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Number of ways to split a binary number such that every part is divisible by 2
- Sum of all second largest divisors after splitting a number into one or more parts
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Break a number such that sum of maximum divisors of all parts is minimum
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Largest and smallest digit of a number
- Largest subset with M as smallest missing number
- Spt function or Smallest Parts Function of a given number
- Smallest and Largest N-digit number starting and ending with N
- Queries for the smallest and the largest prime number of given digit
- Find if a number is part of AP whose first element and difference are given
- Split Array into min number of subsets with difference between each pair greater than 1
- Smallest number dividing minimum number of elements in the Array
- Smallest number dividing minimum number of elements in the array | Set 2
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts