Skip to content

Commit 1b1faba

Browse files
author
emtiazzahid
committed
utopian-tree solved
1 parent 85f1c10 commit 1b1faba

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

OUTPUT_PATH.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

utopian-tree.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
// Complete the utopianTree function below.
4+
function utopianTree($n) {
5+
$height = 1;
6+
if ($n > 0){
7+
$i = 1;
8+
while ($i <= $n) {
9+
if ($i % 2 == 0) {
10+
$height += 1;
11+
}
12+
else {
13+
$height *= 2;
14+
}
15+
$i++;
16+
}
17+
}
18+
19+
return $height;
20+
21+
}
22+
23+
$fptr = fopen("OUTPUT_PATH.txt", "w");
24+
25+
$stdin = fopen("php://stdin", "r");
26+
27+
fscanf($stdin, "%d\n", $t);
28+
29+
for ($t_itr = 0; $t_itr < $t; $t_itr++) {
30+
fscanf($stdin, "%d\n", $n);
31+
32+
$result = utopianTree($n);
33+
34+
fwrite($fptr, $result . "\n");
35+
}
36+
37+
fclose($stdin);
38+
fclose($fptr);

0 commit comments

Comments
 (0)