I am having an array value of x numbers, and want output as two arrays that will be closest possible to an average of the main array.
for ex: arr = [1,5,9,14]
so avg(arr) = 7.25
now possible combinations = [1+5/2 ; 1+9/2 ; 1+14/2 ; 5+9 /2 ; 9+14/2]
so avg of all = [3,5,7.5,7,11.5]
the closest possible values are 7 & 7.5 (the output i am expecting)
now the same is possible with arrays of 8 values [1,3,4,6,7,8,5,6] avg = 5; here again, I want to make only two arrays of 4 values each having nearest possible average.
I have tried with the code but still unsure which all math function can help me here:
$temp_data2 = array_map("unserialize", array_unique(array_map("serialize", $temp_data2)));
foreach($temp_data2 as $k => $v){
$diff[abs(10)] = $v;
}
ksort($diff, SORT_NUMERIC);
$first_pair = current($diff);
print_r($first_pair."::");
print_r($first_pair['combi']);
print_r($temp_data2);
//SECOND PAIR
$temp_data3 = array();
$temp_data3 = $temp_data2;
$temp_data3 = array_map("unserialize",
array_unique(array_map("serialize", $temp_data3)));
foreach($temp_data3 as $k => $v){
$diff[abs(10)] = $v;
}
ksort($diff, SORT_NUMERIC);
$second_pair = current($diff);
print_r($second_pair);
print_r($temp_data3);