John Davidson

php - maximum number of possibility

0 comments
Message:


I need to get the max possible from array that has the quantity of each product.
In the calculation of possibilities, there must always be two products, it cannot be less or more.


// array products
$group_iten = array (1,1,4); // 6 units in 3 products

// max and min per product
$max_min_products = 2;

// my algorithm
$i = 0;
$c = 0;
$counter = 0;
while ( true ) {
// Sorts array in place by values in descending order
rsort($group_iten);

if ( $c < $max_min_products ) {

$group_iten[$i] -= 1;

$i++;
$c++;
$counter++;
}
else {
$c = 0;
$i = 0;
}

if ( min($group_iten) == 0 ) {
unset($group_iten[$i]);
}

if ( count($group_iten) < $max_min_products )
break;
}
print_r($counter);
// result = 2

line output:
Array sorted:
4, 1, 1
3, 1, 0 ( 1 )
2, 0, 0 ( 2 )
End;
result = 2

But with example array = [4, 4, 2];
4, 4, 2 ( inital array )
3, 3, 2 ( 1 )
2, 2, 2 ( 2 )
1, 1, 2 ( 3 )
1, 0, 1 ( 4 )
0, , 0 ( 5 )

Expected: result 5, but my algorithm result 8.



My Answer:
< count($group_iten) ) {_ $group_iten[$c] -= $max_min_products; $group_iten[$c+1] -= $max_min_products; $counter++; $i++; } else { $c++; } if ( $c == count($group_iten) ) { break; }}echo "Maximum number of possibilities: " . $counter;

Rate this post

3 of 5 based on 3084 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us