Skip to content

Commit a1cdf8c

Browse files
Merge pull request #50 from Chaitanyasuma/patch-12
Create UtopianTree.java
2 parents c795ea5 + bbd6733 commit a1cdf8c

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

hackerrank/java/UtopianTree.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Link: https://www.hackerrank.com/challenges/utopian-tree/problem
2+
3+
import java.io.*;
4+
import java.math.*;
5+
import java.security.*;
6+
import java.text.*;
7+
import java.util.*;
8+
import java.util.concurrent.*;
9+
import java.util.regex.*;
10+
11+
public class Solution {
12+
13+
// Complete the utopianTree function below.
14+
static int utopianTree(int n) {
15+
int h=1;
16+
for(int i=1;i<=n;i++)
17+
{
18+
if(i%2!=0)
19+
{
20+
h=h*2;
21+
}
22+
23+
else
24+
{
25+
h=h+1;
26+
}
27+
}
28+
29+
return h;
30+
31+
}
32+
33+
private static final Scanner scanner = new Scanner(System.in);
34+
35+
public static void main(String[] args) throws IOException {
36+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
37+
38+
int t = scanner.nextInt();
39+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
40+
41+
for (int tItr = 0; tItr < t; tItr++) {
42+
int n = scanner.nextInt();
43+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
44+
45+
int result = utopianTree(n);
46+
47+
bufferedWriter.write(String.valueOf(result));
48+
bufferedWriter.newLine();
49+
}
50+
51+
bufferedWriter.close();
52+
53+
scanner.close();
54+
}
55+
}

0 commit comments

Comments
 (0)