Skip to content

Commit 334838a

Browse files
Merge pull request jitendrajat10099#17 from rightturn/algo_apple_and_orange
Add Easy Hackerrank Solutions in php (Easy)
2 parents cd6cba5 + 47b821f commit 334838a

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
Link - https://www.hackerrank.com/challenges/apple-and-orange/problem
5+
*/
6+
// Complete the countApplesAndOranges function below.
7+
function countApplesAndOranges($s, $t, $a, $b, $apples, $oranges) {
8+
9+
$apple_catch = 0;
10+
$orange_catch = 0;
11+
12+
foreach($apples as $apple){
13+
if ($apple >= 0) {
14+
if ($apple+$a >=$s && $apple+$a <=$t) {
15+
$apple_catch++;
16+
}
17+
}
18+
}
19+
20+
foreach($oranges as $orange){
21+
if($orange <= 0){
22+
if($b+$orange <=$t && $b+$orange >=$s ){
23+
$orange_catch++;
24+
}
25+
}
26+
27+
}
28+
29+
echo $apple_catch . "\n";
30+
echo $orange_catch . "\n";
31+
32+
}
33+
34+
$stdin = fopen("php://stdin", "r");
35+
36+
fscanf($stdin, "%[^\n]", $st_temp);
37+
$st = explode(' ', $st_temp);
38+
39+
$s = intval($st[0]);
40+
41+
$t = intval($st[1]);
42+
43+
fscanf($stdin, "%[^\n]", $ab_temp);
44+
$ab = explode(' ', $ab_temp);
45+
46+
$a = intval($ab[0]);
47+
48+
$b = intval($ab[1]);
49+
50+
fscanf($stdin, "%[^\n]", $mn_temp);
51+
$mn = explode(' ', $mn_temp);
52+
53+
$m = intval($mn[0]);
54+
55+
$n = intval($mn[1]);
56+
57+
fscanf($stdin, "%[^\n]", $apples_temp);
58+
59+
$apples = array_map('intval', preg_split('/ /', $apples_temp, -1, PREG_SPLIT_NO_EMPTY));
60+
61+
fscanf($stdin, "%[^\n]", $oranges_temp);
62+
63+
$oranges = array_map('intval', preg_split('/ /', $oranges_temp, -1, PREG_SPLIT_NO_EMPTY));
64+
65+
countApplesAndOranges($s, $t, $a, $b, $apples, $oranges);
66+
67+
fclose($stdin);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
Link - https://www.hackerrank.com/challenges/birthday-cake-candles/problem
5+
*/
6+
7+
// Complete the birthdayCakeCandles function below.
8+
function birthdayCakeCandles($arr) {
9+
$max = 1;
10+
$max_count = 0;
11+
12+
for($i=0;$i<count($arr);$i++){
13+
if($max < $arr[$i]){
14+
$max = $arr[$i];
15+
}
16+
}
17+
18+
for($i=0;$i<count($arr);$i++){
19+
if($max == $arr[$i]){
20+
$max_count++;
21+
}
22+
}
23+
24+
return $max_count;
25+
26+
}
27+
28+
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
29+
30+
$stdin = fopen("php://stdin", "r");
31+
32+
fscanf($stdin, "%d\n", $ar_count);
33+
34+
fscanf($stdin, "%[^\n]", $ar_temp);
35+
36+
$ar = array_map('intval', preg_split('/ /', $ar_temp, -1, PREG_SPLIT_NO_EMPTY));
37+
38+
$result = birthdayCakeCandles($ar);
39+
40+
fwrite($fptr, $result . "\n");
41+
42+
fclose($stdin);
43+
fclose($fptr);

0 commit comments

Comments
 (0)