-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplesAndOranges.php
73 lines (55 loc) · 1.92 KB
/
applesAndOranges.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/*
* Complete the 'countApplesAndOranges' function below.
*
* The function accepts following parameters:
* 1. INTEGER s
* 2. INTEGER t
* 3. INTEGER a
* 4. INTEGER b
* 5. INTEGER_ARRAY apples
* 6. INTEGER_ARRAY oranges
*/
function countApplesAndOranges($s, $t, $a, $b, $apples, $oranges)
{
//set counter for apples and oranges
$countApples = 0;
$countOranges = 0;
//loop through apples array
foreach ($apples as $apple) {
//sum apple distance with tree position to know wheter the apples falls into house or not
$applePos = $apple + $a;
//check if the apple position matches with house position
if ($applePos >= $s && $applePos <= $t) {
//add counter if position match
$countApples++;
}
}
//loop through oranges array
foreach ($oranges as $orange) {
//sum oranges distance with tree position to know wheter the oranges falls into house or not
$orangePos = $orange + $b;
//check if the oranges position matches with house position
if ($orangePos >= $s && $orangePos <= $t) {
//add counter if position match
$countOranges++;
}
}
//show output
echo $countApples . "\n" . $countOranges;
//done
}
$first_multiple_input = explode(' ', rtrim(fgets(STDIN)));
$s = intval($first_multiple_input[0]);
$t = intval($first_multiple_input[1]);
$second_multiple_input = explode(' ', rtrim(fgets(STDIN)));
$a = intval($second_multiple_input[0]);
$b = intval($second_multiple_input[1]);
$third_multiple_input = explode(' ', rtrim(fgets(STDIN)));
$m = intval($third_multiple_input[0]);
$n = intval($third_multiple_input[1]);
$apples_temp = rtrim(fgets(STDIN));
$apples = array_map('intval', preg_split('/ /', $apples_temp, -1, PREG_SPLIT_NO_EMPTY));
$oranges_temp = rtrim(fgets(STDIN));
$oranges = array_map('intval', preg_split('/ /', $oranges_temp, -1, PREG_SPLIT_NO_EMPTY));
countApplesAndOranges($s, $t, $a, $b, $apples, $oranges);