Random list of M non-negative integers whose sum is N - GeeksforGeeks

19 Mar, 2022
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 = 8Input: 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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Utility function to print the// elements of an arrayvoid 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 nvoid 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 codeint main(){    int m = 4, n = 8;     randomList(m, n);     return 0;}
// Java implementation of the approachclass GFG{     // Utility function to print the// elements of an arraystatic 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 nstatic 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 codepublic static void main(String args[]){    int m = 4, n = 8;     randomList(m, n);}} // This code is contributed by Arnab Kundu
# Python3 implementation of the approachfrom random import randint # Utility function to print the# elements of an arraydef 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 ndef 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 codeif __name__ == "__main__" :     m = 4; n = 8;     randomList(m, n); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{     // Utility function to print the// elements of an arraystatic 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 nstatic 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 codepublic static void Main(){    int m = 4, n = 8;     randomList(m, n);}} // This code is contributed by Mohit kumar
<script>// Javascript implementation of the approach         // Utility function to print the// elements of an array    function printArr(arr,n)    {        for (let i = 0; i < n; i++)            document.write(arr[i] + " ");       }         // Function to generate a list of// m random non-negative integers// whose sum is n    function randomList(m,n)    {        // Create an array of size m where    // every element is initialized to 0    let arr = new Array(m);    for(let i=0;i<arr.length;i++)    {        arr[i]=0;    }         // To make the sum of the final list as n    for (let i = 0; i < n; i++)    {           // Increment any random element        // from the array by 1        arr[(Math.floor((Math.random() * n) )%m)]++;    }       // Print the generated list    printArr(arr, m);    }         // Driver code    let m = 4, n = 8;    randomList(m, n);           // This code is contributed by patel2127</script>
1 3 3 1
 
Time Complexity: O(max(M, N))
Auxiliary Space: O(M) 
ankthon
andrew1234
mohit kumar 29
patel2127
subham348
Numbers
Analysis
Greedy
Mathematical
Greedy
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
3-coloring is NP Complete
Proof that Dominant Set of a Graph is NP-Complete
Difference between Deterministic and Non-deterministic Algorithms
Set partition is NP complete
Dijkstra's shortest path algorithm | Greedy Algo-7
Program for array rotation
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3