Skip to content

Instantly share code, notes, and snippets.

@flymio
Created January 21, 2019 15:31
Show Gist options
  • Save flymio/8c308a6b2044d7bf7c59ee302d57e668 to your computer and use it in GitHub Desktop.
Save flymio/8c308a6b2044d7bf7c59ee302d57e668 to your computer and use it in GitHub Desktop.
<?php
// Complete the birthday function below.
function birthday($s, $d, $m) {
$tot = 0;
for($i=0;$i<=sizeof($s)-1;$i++){
$cnt=1;$sum=$s[$i];$ok=0;
for($j=$i+1;$j<=sizeof($s)-1;$j++)
{
$cnt++;
if ($cnt<=$m && !$ok){
$sum+=$s[$j];
if ($sum==$d && $cnt==$m){
$ok=1;
$tot++;
}
}
}
if ($sum==$d && $cnt==$m && !$ok){
$tot++;
}
}
//echo "[$tot]\n";
return $tot;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$n = intval(trim(fgets(STDIN)));
$s_temp = rtrim(fgets(STDIN));
$s = array_map('intval', preg_split('/ /', $s_temp, -1, PREG_SPLIT_NO_EMPTY));
$dm = explode(' ', rtrim(fgets(STDIN)));
$d = intval($dm[0]);
$m = intval($dm[1]);
$result = birthday($s, $d, $m);
fwrite($fptr, $result . "\n");
fclose($fptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment