I have to create an algorithm that must take this input: N, range (rMax and rMin value) and average. And in function of this, it must return N values (an array for example) whose average is average input value.
function createAverageValues(N,rMin,rMax,average){
var averageValues = [];
var j = 0;
while(j<N){
....
....
....
averageValues.push(...);
j++;
};
return averageValues;
};
Requirements:
- N, rMin, rMax are integer input values;
- Return values can be also float values with two decimal places (x.xx).
- Whenever I use the algorithm, with the same input values, the returned values can be different. But their average must always be the one indicated in the input;
- The returned values, can also be repeated several times. So can also use several times the same value. I only care that average of returned averageValues array is the one required.
Exemple 1
/**
N of values: 4;
Range: 3-7 (3 <= value <= 7);
Average: 5;
N = 4;
rMin = 3;
rMax = 7;
average = 5;
**/
var averageValues = createAverageValues(4,3,7,5);
One possible solution would be:
averageValues = [3,4,6,7];
Another possible solution would be:
averageValues = [6,4,4,6];
Etc...
Exemple 2
/**
N of values: 5;
Range: 0-12 (0 <= value <= 12);
Average: 6;
N = 5;
rMin = 0;
rMax = 12;
average = 6;
**/
var averageValues = createAverageValues(5,0,12,6);
One possible solution would be:
averageValues = [12,4,10,0,4];
Another possible solution would be:
averageValues = [10,8,11,0,1];
Etc...
Exemple 3
/**
N of values: 3;
Range: 9-15 (9 <= value <= 15);
Average: 12;
N = 3;
rMin = 9;
rMax = 15;
average = 12;
**/
var averageValues = createAverageValues(3,9,15,12);
One possible solution would be:
averageValues = [10,14,12];
Another possible solution would be:
averageValues = [11,11,14];
Another possible solution would be:
averageValues = [9.50,14.75,11.75];
Etc...