There is a lottery with n coupons and n people take part in it. Each person picks exactly one coupon. Coupons are numbered consecutively from 1 to n, n being the maximum ticket number. The winner of the lottery is any person who owns a coupon where the sum of the digits on the coupon is equal to s. If there are multiple winners, the prize is split equally among them. Determine how many values of s there are where there is at least one winner and the prize is split among most people.
Example
n = 12
The list of coupon numbers generated from 1 to nis [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]. The sums of the digits are [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3]. The largest number of winners is 2 which will occur for coupons numbered [1, 10], [2, 11] and [3, 12]. The maximum number of possible winners occurs for any of these 3 possible values of s, so 3 is the answer.
CodePudding user response:
<?php
$arr = array();
$lenArr = array();
for($j = 1; $j < 12; $j ) {
$sum = 0;
$j = (String)$j;
for ($i = 0; $i < strlen((String)$j); $i ) {
$sum = $j[$i];
}
if(!isset($arr[$sum])) {
$arr[$sum] = 1;
} else {
$arr[$sum] ;
}
}
$max = max($arr);
$returnVal = 0;
foreach($arr as $key => $value) {
if($max == $value) {
$returnVal ;
}
}
return $returnVal;
?>
CodePudding user response:
If I am understanding the task properly, you need to:
- iterate a range of numbers up to
n
, and sum the digits then - count how many times a given ticket value occurs, then
- count how many times winning groups of each size occurs, then
- find the count of the largest group.
Code: (Demo)
$n = 12;
$array = array_map(fn($v) => array_sum(str_split($v)), range(1, $n));
$winningGroupCounts = array_count_values(array_count_values($array));
echo "number of largest winning group(s): " . $winningGroupCounts[max(array_keys($winningGroupCounts))];
Output:
number of largest winning group(s): 3