Given two integers M and N, the task is to create a list of M non-negative integers whose sum is N. In case when more than one list is possible, find any one.
Input: M = 4, N = 8
Output: 1 3 3 1
1 + 3 + 3 + 1 = 8
Input: M = 5, N = 3
Output: 0 1 1 0 1
Approach: To get a complete random list of integers, create an array of size M where every element is initialised with 0. Now run a loop from 0 to N – 1 and increment any randomly chosen element from the array by 1 using the rand() function. This way, sum of the resuntant list will be N.
Below is the implementation of the above approach:
1 3 3 1
Time Complexity: O(max(M, N))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Move all zeros to start and ones to end in an Array of random integers
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Number of arrays of size N whose elements are positive integers and sum is K
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Count of subsequences whose product is a difference of square of two integers
- Count of pairs of integers whose difference of squares is equal to N
- Split N into two integers whose addition to A and B makes them equal
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Maximum sum of two elements whose digit sum is equal
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Select a random number from stream, with O(1) space
- Random number generator in arbitrary probability distribution fashion
- Pseudo Random Number Generator (PRNG)
- Binomial Random Variables
- Erdos Renyl Model (for generating Random Graphs)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.