Sie sind auf Seite 1von 7

Problem Set(29-Oct-2011)

Problem A : Crackers

A small child (very cute) went for shopping crackers in a mall for Diwali. All the crackers were kept for a display in a queue. Child was very intelligent he can guess the exact height of the cracker by seeing it. He was also stratergic, he will only take a cracker of more or equall height than what he already purchase. He wants to buy maximum number of crackers by making a single pass of a queue.

Can you guys please help that child to purchase the crackers in such a way that he gets maximum number of crackers and that too in the non-decreasing order of height.

Input:

First line contains N(total numbers of crackers). Second line contains the height of N crackers.(Height can be negative also)

Constraints:

1 <= N <= 3000 -10^5 <= Height <= 10^5 Time Limit : 1s

Output:

Maximum numbers of crackers. Print the answer in a single line followed by a '\n' ( just print a new line no need to display '\n' )

Sample Case 1:

Input:

10

6 3 4 8 10 5 7 1 9 2 Output:

5\n

Explaination:

Child will buy crackers of height 3,4,5,7,9

Sample Case 2:

Input:

10

-6 3 -4 8 -4 5 -1 1 9 2

Output:

6\n

Explaination:

Problem B : Make Incs

You are given an array A[1

element of the array ( i.e., A[i] = A[i] + K ). Find the minimum steps required to make the array, an

(strictly) increasing sequence, i.e., A[1] < A[2] < A[3] ,

n]

of postive integers and an integer K. In each step, you can add K to any

, A[n-1] < A[n].

Input:

First line contains N and K and second line contains the array A[1 N].

Output:

Print the answer in a single line, followed by a '\n' ( just print a new line, no need to display '\n' )

Constraints:

1 <= N <= 1000

1 <= K <= 106

1 <= A[i] <= 106

Time Limit : 1s Sample Case 1:

Input:

4 2

1 3 3 2

Output:

3\n

Explanation:

A[1

A[4] = A[4] + 2 -> {1,3,3,4} A[3] = A[3] + 2 -> {1,3,5,4} A[4] = A[4] + 2 -> {1,3,5,6}

4]

= {1,3,3,2}

Problem C : AIBOHPHOBIA

Your task is, given a string S, check whether it is a palindrome or not. Easy

isn't it ?

Wait, here is a twist if its not a palindrome you have to split it in to palindromes in such a way that their concatenation gives S. Still easy ?

Now to make this task really good, you have to split in such a way that minimum number of palindromes are formed. In other words, you have to find the minimum number of palindromic pieces, whose concatenation is S

Constraints:

1 <= Length of String (S)<= 5600 Time Limit : 1s

Input:

Single Line contains a String S which will only contain [ 'A' - 'Z' ].

Output:

Print the minimum number of palindromes in a single line, followed by a '\n' ( just print a new line, no need to display '\n' )

Sample Case 1:

Input:

REDIVIDER

Output:

1\n

Explanation:

No need to Split, as its already a palindrome.

Sample Case 2:

Input:

APSISCOOL

Output:

6\n

Explanation:

Following are the pieces, A + P + SIS + C + OO + L --> APSISCOOL ( total 6 pieces).

Problem D : Ra.2

A binary string is called Ra.Two if it has no two consecutive ones in it and begins with a 1. The first few such binary strings in order are {1, 10, 100, 101, 1000, 1001, 1010, 10000, 10001, 10010, 10100,

 10101 }. Given N, find the Nth smallest Ra.Two number. Input:

First line contains N.

Output:

Print the answer in a single line, followed by a '\n' ( just print a new line, no need to display '\n' )

Constraints:

1 <= N <= 109 Time Limit : 1s

Sample Case 1:

Input:

5

Output:

1000\n

Sample Case 2:

Input:

40

Output:

10001001\n

Problem E : Islands

Given the map of Lakshadweep, find the number of islands in it. You will be given a rectangular map of R rows and C columns. Each cell is either land ( denoted by '#' ) or water ( denoted by '.' ). Two cells are adjacent to eachother, if they share a side. So there can be at most 4 neighbors to each cell ( diagonal cells are not treated adjacent ). Now, an island is a piece of connected land. Find the number of islands present. See the example cases for more clarity.

Input:

First line contains R and C. Next R lines give the description of the island.

Output:

Print the answer in a single line, followed by a '\n' ( just print a new line, no need to display '\n' )

Constraints:

1 <= R, C <= 200

Each cell is either a '.' or a '#' Time Limit : 1s

Sample Case 1:

Input:

3 4

#

##.#

#

#

Output:

2\n

Sample Case 2:

Input:

3 3

#

#

.##

Output:

3\n

Problem F : Height of BST

Given a sequence of distinct numbers, make a BST by inserting them in the given order. Find the height of the resulting tree. Note : Height of a single node tree is 1.

Constraints:

1 <= N <= 10^4

Time Limit : 1s

Input:

First Line Contain single number N denoting the number of nodes. Next line contains N distinct numbers separated by space.

Output:

Print the height of the tree, followed by a '\n' ( just print a new line, no need to display '\n' )

Sample Case 1:

Input:

5

 2 1 4 8 Output: 7 3\n Sample Case 2: Input: 5 1 2 3 4 5

Output:

5\n

Problem G : Level Order BST

Given a sequence of distinct numbers, make a BST by inserting them in the given order. Print the Level Order Traversal of the resulting tree.

Constraints:

1 <= N <= 10^4

Time Limit : 1s

Input:

First Line Contain single number N denoting the number of nodes. Next line contains N distinct numbers separated by space.

Output:

Print the Level Order Traversal of the tree, followed by a '\n' ( just print a new line, no need to display '\n' ) i.e A space follows each number in level-order ( including the last one ) and finally a \n

 Sample Case 1: Input: 5 7 2 1 4 8

Output:

7 2 8 1 4 \n

Please notice the last space after the last node

 Sample Case 2: Input: 5 1 2 3 4 5

Output:

1 2 3 4 5 \n