Skip to main content

Utopian Tree [JAVA]

Problem Statement
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.
Now, a new Utopian tree sapling is planted at the onset of the spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?
Input Format
The first line contains an integer, T, the number of test cases.
T lines follow. Each line contains an integer, N, that denotes the number of cycles for that test case.
Constraints
1 <= T <= 10
0 <= N <= 60
Output Format
For each test case, print the height of the Utopian tree after N cycles.


 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
* @author Rakesh KR
*/
public class Solution {

 public static void main(String[] args) throws Exception {

  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  String numberOfTestCases = in.readLine();
  if(numberOfTestCases.matches("[1-9]|10")){
   for(int i=0;i<Integer.parseInt(numberOfTestCases);i++){
    String num = in.readLine();
    long val = Long.parseLong(num);
    if(val >=0 && val <=60){
     int count = 0;
      for(int j=1 ;j<=val+1; j++){
       count = (j%2==0) ? count * 2 : count + 1 ;
      }
     System.out.println(count);
    }
   }
  }
 }

}

Comments

Popular posts from this blog

Transform Ubuntu into Xubuntu

Installation 1. Open the terminal by Ctrl+Alt+T     and type    sudo apt-get install xubuntu-desktop gksu leafpad synaptic         2. Type your password & Press Enter. Now an intensive operation is being launched. Simply wait to complete the whole process. Login To Xubuntu 1. After completing the installation logout ubuntu. Note: logout not restart or shutdown. 2. In the login window click on the ubuntu logo, next to your userName & select Xubuntu Sesion 3. Enter your password and Now the Xubuntu desktop appears. :) The next thing is to clean up. Clean Up 1. Now its time to clean up, inorder to prevent system pollution problems. Note: The clean up will remove as much as possible ubuntu's desktop environment Unity. So after that you can't use Unity. 2. Open terminal by Ctrl+Alt+T and type the following  sudo apt-get remove nautilus gnome-power-manager gnome-screensaver gnome-termina*...

Object serialization in java

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object. When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Code for serialization of a java class : Data.java package com . codetalk . serialization ; import java.io.Serializable ; public class Data implements Serializable { private static final long serialVersionUID = 1L; private String firstName ; private String lastName ; /** * @return the firstName */ public String getFirstName () { return firstName ; } /** * @param firstNam...

PostgreSQL :: open-source relational database management system (RDBMS)

 PostgreSQL also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance.  PostgreSQL features transactions with ACID properties, automatically updatable views, materialized views, triggers, foreign keys, and stored procedures. It is designed to handle a range of workloads, from single machines to data warehouses or Web services with many concurrent users. PostgreSQL manages concurrency through multiversion concurrency control (MVCC), which gives each transaction a "snapshot" of the database, allowing changes to be made without affecting other transactions.  PostgreSQL provides an asynchronous messaging system that is accessed through the NOTIFY, LISTEN and UNLISTEN commands. PostgreSQL includes built-in support for regular B-tree and hash table indexes, and four index access methods: generalized search trees (GiST), generalized inverted indexes (GIN), Space-Partitioned GiST (SP-GiST) and Bloc...