lundi 19 avril 2021

Generating an integer array consisting of N random integers that should give the sum S

So, basically I want to generate an integer array of size N, consisting of random generated values.

Also, I have to generate them in such way, that the sum of the elements will be S.

For example, if I get:

int N = 5;
int S = 10;
int[] generatedArray = new int[N];

...some computations...

Then the result should be something like:

int[] generatedArray = {2,1,4,3,0} //2+1+4+3+0=10, and they are 5 integers

The solution to this problem should be as optimized as possible.

Also, the solution should be specifically in Java. You can provide universal solutions, but not to depend on any other language than Java.




Aucun commentaire:

Enregistrer un commentaire