Random list of M non-negative integers whose sum is N
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.
Examples:
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print the // elements of an array void printArr(int arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Function to generate a list of // m random non-negative integers // whose sum is n void randomList(int m, int n) { // Create an array of size m where // every element is initialized to 0 int arr[m] = { 0 }; srand(time(0)); // To make the sum of the final list as n for (int i = 0; i < n; i++) { // Increment any random element // from the array by 1 arr[rand() % m]++; } // Print the generated list printArr(arr, m); } // Driver code int main() { int m = 4, n = 8; randomList(m, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Utility function to print the // elements of an array static void printArr(int arr[], int n) { for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Function to generate a list of // m random non-negative integers // whose sum is n static void randomList(int m, int n) { // Create an array of size m where // every element is initialized to 0 int arr[] = new int[m]; // To make the sum of the final list as n for (int i = 0; i < n; i++) { // Increment any random element // from the array by 1 arr[(int)(Math.random() * m)]++; } // Print the generated list printArr(arr, m); } // Driver code public static void main(String args[]) { int m = 4, n = 8; randomList(m, n); } } // This code is contributed by Arnab Kundu |
Python3
# Python3 implementation of the approach from random import randint # Utility function to print the # elements of an array def printArr(arr, n) : for i in range(n) : print(arr[i], end = " "); # Function to generate a list of # m random non-negative integers # whose sum is n def randomList(m, n): # Create an array of size m where # every element is initialized to 0 arr = [0] * m; # To make the sum of the final list as n for i in range(n) : # Increment any random element # from the array by 1 arr[randint(0, n) % m] += 1; # Print the generated list printArr(arr, m); # Driver code if __name__ == "__main__" : m = 4; n = 8; randomList(m, n); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Utility function to print the // elements of an array static void printArr(int []arr, int n) { for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Function to generate a list of // m random non-negative integers // whose sum is n static void randomList(int m, int n) { // Create an array of size m where // every element is initialized to 0 int [] arr = new int[m]; // To make the sum of the final list as n for (int i = 0; i < n; i++) { // Increment any random element // from the array by 1 Random rnd = new Random(); arr[rnd.Next(0, n) % m]++; } // Print the generated list printArr(arr, m); } // Driver code public static void Main() { int m = 4, n = 8; randomList(m, n); } } // This code is contributed by Mohit kumar |
1 3 3 1
Time Complexity: O(max(M, N))
Recommended Posts:
- Move all zeros to start and ones to end in an Array of random integers
- Median in a stream of integers (running integers)
- Binomial Random Variables
- Pseudo Random Number Generator (PRNG)
- Select a random number from stream, with O(1) space
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Erdos Renyl Model (for generating Random Graphs)
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Random number generator in arbitrary probability distribution fashion
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Sum of last digit of all integers from 1 to N divisible by M
- Armstrong Numbers between two integers
- Find the prime P using given four integers
- Sum of f(a[i], a[j]) over all pairs in an array of n integers
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.



