Friday, 24 October 2014

Service Lane in java (HackerRank)

Problem Statement: 

Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the highway and the service lane is N units. The service lane consists of N segments of unit length, where each segment can have different widths.

Calvin can enter into and exit from any segment. Let's call the entry segment as index i and the exit segment as index j. Assume that the exit segment lies after the entry segment(j>i) and i ≥ 0. Calvin has to pass through all segments from index i to index j (both inclusive).

 
Service Lane Example

Calvin has three types of vehicles - bike, car and truck, represented by 1, 2 and 3 respectively. These numbers also denote the width of the vehicle. We are given an array width[] of length N, where width[k] represents the width of kth segment of our service lane. It is guaranteed that while servicing he can pass through at most 1000 segments, including entry and exit segments.
  • If width[k] is 1, only the bike can pass through kth segment.
  • If width[k] is 2, the bike and car can pass through kth segment.
  • If width[k] is 3, any of the bike, car or truck can pass through kth segment.
Given the entry and exit point of Calvin's vehicle in the service lane, output the type of largest vehicle which can pass through the service lane (including the entry & exit segment)

Input Format

The first line of input contains two integers - N & T, where N is the length of the freeway, and T is the number of test cases. The next line has N space-separated integers which represents the width array.
T test cases follow. Each test case contains two integers - i & j, where i is the index of segment through which Calvin enters the service lane and j is the index of the lane segment where he exits.

Output Format

For each test case, print the number that represents the largest vehicle type that can pass through the service lane.

Note

Calvin has to pass through all segments from index i to indexj (both inclusive).

Constraints

2 <= N <= 100000
1 <= T <= 1000
0 <= i < j < N
2 <= j-i+1 <= min(N,1000)
1 <= width[k] <= 3, where 0 <= k < N 

Sample Input #00

8 5
2 3 1 2 3 2 3 3
0 3
4 6
6 7
3 5
0 7
 

Sample Output #00

1
2
3
2
1
 

Explanation for Sample Case #0

Below is the representation of lane.
   |HIGHWAY|Lane|    ->    Width

0: |       |--|            2
1: |       |---|           3
2: |       |-|             1
3: |       |--|            2
4: |       |---|           3
5: |       |--|            2
6: |       |---|           3
7: |       |---|           3
 
  1. (0, 3): Because width[2] = 1, only the bike represented as 1 can pass through it.
  2. (4, 6): Here the largest allowed vehicle which can pass through the 5th segment is car and for the 4th and 6th segment it's the truck. Hence the largest vehicle allowed in these segments is a car.
  3. (6, 7): In this example, the vehicle enters at the 6th segment and exits at the 7th segment. Both segments allow even truck to pass through them. Hence truck is the answer.
  4. (3, 5): width[3] = width[5] = 2. While 4th segment allow the truck, 3rd and 5th allow upto car. So 2 will be the answer here.
  5. (0, 7): Bike is the only vehicle which can pass through the 2nd segment, which limits the strength of whole lane to 1.

  Algorithm or Steps : 

1.  First we accept Input string which is string and contains length of freeway and number of test cases separated by space(' ').
2.  Then we split string using split() function. and store length of freeway as 'len' and number of test cases as 'test'.
3.  Create 3 arrays which stores lane info, test case start and test case end.
4.  Accept lane info and store it in width[].

5.  Accept test case string which is separated by space and store it in two arrays as start[] and end[].
6.  Then for each test case we check what is maximum number of width of lane. and display maximum length of width.

 

 Program :

Service Lane Program in Java (HackerRank)

 Output :

Output of Service Lane program in Java

 

 To download code click here.

For beginners in java please see this link. 

 

Sunday, 28 September 2014

Utopian Tree Program in java

Problem Statement:

        The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the monsoon, 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 monsoon. 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.

Sample Input #00:
2
0
1
Sample Output #00:
1
2

Explanation #00:
        There are 2 test cases. When N = 0, the height of the tree remains unchanged. When N = 1, the tree doubles its height as it's planted just before the onset of monsoon.

Sample Input: #01:
2
3
4
Sample Output: #01:
6
7
Explanation: #01:

There are 2 testcases.
N = 3:
the height of the tree at the end of the 1st cycle = 2
the height of the tree at the end of the 2nd cycle = 3
the height of the tree at the end of the 3rd cycle = 6
N = 4:
the height of the tree at the end of the 4th cycle = 7

Tuesday, 6 May 2014

Reset MySQL Root Password without knowing Root password

  1. If you have never set a “root” password for MySQL, the server does not require a password at all for connecting as “root”. However, this is insecure.
  2. If you know the root” password, but want to change it.
  3. If you set a “root” password previously, but have forgotten it, you can set a new password. The following sections provide instructions for Windows systems. 
 To know more about MySQL click here. To download latest versions of MySQL click here.
You can try different methods like uninstalling MySql & reinstall it. But while reinstalling, it will ask previous password. To avoid this I will tell you one simple method to change MySql password without knowing previous password & save our work as it is.


Steps 1.  Stop MySql service.


                   For that Right click on task bar --> Start Task Manager -->
 
Start Task Manager

                   Services -->
Select Services
                      MySQL --> Right Click --> Stop
Select MySQL service


Stop MySQL service

Step 2:-



Create .txt file contain following contain where ‘nikhil’ is new password for your MySQL. You can change this as per your requirement.

Save this file in C drive and name it as mysql-init
 
.txt file

Step3:-

Open Command Prompt:- Press windows key+R --> Enter cmd --> OK
 
start RUN
           
           You will see following window.

Start Command Prompt

       Go to root directory. By using following steps.

Go To Root Directory
          Then you have to write following command in command prompt. 

"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld.exe" --defaults- file="C:\\Program Files\\MySQL\\MySQL Server 5.1\\my.ini" --init-file=C:\\mysql-init.txt

And then press enter.

Execute command

Now you can log in into MySql using ‘nikhil’ as password.
LogIn MySQL with new Password

Note:- While performing all above activity you have to log in as Administrator.
I've tested this on MySQL Server 5.1 Only using Windows 7.