You are on page 1of 7

PROBLEM- SET( 01-Oct-2011)

Problem A : Consistent Strings


You are given an string which can have two characters '(', ')'. You have to check whether the string is consistent or not ? The definition for being consistent is as follows: 1. An empty string is consistent. 2. If STR is consistent, then (STR) is also consistent. 3. If S1 and S2 are consistent, then S1S2 (the concatenation of two) is also consistent. All of these strings are consistent: (), ()(), and (()()); But none of these: )(, (()(, nor ()(. Your program will be tested on one or more data sets(N).Each data set is described on a single line. No string has more than 1000 ( '(' or ')' ) characters. Input: Line one contains number of testcases N followed by N strings. Output: for each test case print YES\n if string is a consistent else NO\n if not consistent. Constraints: 1 <= N <= 50 string length <= 1000 Time Limit : 1s Sample Input: Input: 3 ()()() (()()) ()()) Output: YES YES NO\n Explanation: By using the rules you can prove string 1 & 2 are consistent but not string 3.

Problem B : Place the Big Banner


Though there was a bandh yesterday, the software companies in cyberabad were working just like a normal day :). Knowing this, some angry representatives of a political party have decided to place a big banner 'Jai Telangana' over the company buildings. There are N buildings in a row, and for simplcity assume the width of each building as 1 unit, and you are given the array heights H of these buildings in order. They want to place a rectangular banner such that each part of the banner is on some building. Can you help them in finding out the maximum area of banner they can place. See the sample case 1 and its figure for more clarity. Input: First line contains N and second line contains the array H. 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 <= 105 1 <= H[i] <= 109 Time Limit : 1s Sample Case 1: Input: 6 365624 Output: 15\n Explanation: As shown in the below figure. Sample Case 2: Input: 6 172531 Output: 8\n Explanation: A banner of height 2, extending from building 2 ( height 7 ) to building 5 ( height 3 ).

Problem C : Consistent Strings Again


You already done with parenthesization problem. Now this time you are given an string which can have 6 characters '(', ')','{','}','[',']' You have to check whether the string is consistent or not ? The definition for being consistent is as follows: 1. An empty string is consistent. 2. If STR is consistent, then each of (STR), [STR], {STR} is also consistent. 3. If S1 and S2 are both consistent, then S1S2 (the concatenation of the two) is also consistent. All of these strings are consistent: (), (){}, and ({}[]); But none of these: )(, {()[, nor []( Your program will be tested on one or more data sets(N).Each data set is described on a single line. No string has more than 1000 characters. Input: Line one contains number of testcases N followed by N strings. Output: for each test case print YES\n if string is a consistent else NO\n if not consistent. Constraints: 1 <= N <= 50 string length <= 1000 Time Limit : 1s Sample Input: Input: 4 (){}[] ({}[]) ({)}[] {[]({[]})} Output: YES YES NO YES\n Explanation: By using the rules you can prove string 1,2 & 4 are consistent but not string 3.

Problem D : Text Editing


A user is typing a sentence using his keyboard. He uses only the keys, 'a' - 'z', H (home) and E(end). Given the sequence of key presses, find the line that is printed on the screen finally. Initially, a cursor is placed at the beginning of empty line and the following actions are performed with each key stroke. - when 'a'-'z' is pressed, its printed at current cursor position after moving the rest of the string to right. For eg. If the current cursor position is at t in the string "deter", pressing a 'x' key results in the string "dexter". - when 'H' is pressed, cursor moves to the beginning. For eg: pressing 'H' when we have "apple" results in string "apple" - when 'E' is pressed, cursor moves to the end of the string. For eg: pressing 'E' when we have "apple" results in string "apple " Its just like any normal editors we use. Try it out pressing 'a' - 'z', Home and End keys to get a feel of it :) Use only C to code this problem. For other problems you can use C/C++ Input: Only one line, contains a long string of at most 100000 valid characters Output: Print the final string in a single line, followed by a '\n' ( just print a new line, no need to display '\n' ) Constraints: Time Limit : 1s Sample Case 1: Input: atHc Output: cat\n Explanation: " " "a " "at " "at" "cat" Sample Case 2: Input: rHsEocHpEkHaEs Output: apsrocks\n Explanation: Of course, whats there to explain... just experience it !

Problem E : Waiting for Dosa


Its thursday night and as usual.. a very long line for Dosa inside NBH mess. There are N students waiting for Dosa, standing in the line. As they are bored, they turn and look to see if any of their batchmates are also standing in the line. Two students A and B standing in the line can see each other if no student between them is strictly taller than A or B. Instead of thinking if you will get a crispy dosa or not, you started thinking about a puzzle ( probably aps effect ! ). How many pairs of students will be able to see each other. Given the heights of all the N students in order, write a program to find it. Input: First line contains an integer N (1 <= N <= 500,000), the number of people standing in line. Each of the following N lines contains a single integer, the height of students, given in the order in which they are standing in line. Each persons height is an integer that fits in an 'int' range. Output: Print the number of pairs of students that can see each other on a single line., followed by a '\n' ( just print a new line, no need to display '\n' ) Constraints: Time Limit : 2s Sample Case 1: Input: 4 4 3 5 2 Output: 4\n Explanation: 4 students with heights {4,3,5,2}. The two pairs (4,2) and (3,2) can't see each other because of the tall guy (5) in between. All other 4 pairs can see each other.

Problem F : Minimum Flips !!!


Given a parenthesis sequence of length N, made up of " ( " and " ) ", you can select any of them and flip it. A flip changes "(" to ")" or ")" to "(". Find the minimum number of flips needed to make the sequence valid. For example : eg: (()( needs 1 change to make it to (()) )( needs 2 changes to make it to () Input: A string containing parenthesis in a single line. Output: Minimum number of flips needed ( followed by a '\n' ). Constraints: n is even and 2 <= n <= 20 Time Limit : 1s Sample Case1: Input: ()(( Output: 1\n Sample Case2: Input: )()()( Output: 2\n

Problem G : Pyramids
You are given an array A[0..N-1].The array element are used to form a pyramid. Your given array A will be the base of pyramid. Each non-base element is sum of immediate lower and lower-right elements. example: say A = {1,2,3,4} . B[0][i] = A[i] and B[i][j] = B[i-1][j] + B[i-1][j+1]. 20 8 12 357 1234 The summation may get very big so use Mod 1000000007. Now you will be given Q queries in which we give the index i of array A[0..N-1] and a value x such that A[i] is updated to x. As the value A[i] is updated the layer above it gets updated and so on... resulting in the topmost value in the pyramid getting updated.So your task is to find the updated top-most value for each query. Input: First line is N Second line contains the N elements i.e. A[i] Third line contains number of query Q. following line contains queries of form "i x" where i is index and x is the value such that A[i] = x Output: Updated Value of top-most value in the pyramid for each query. You have to retain the updation of previous query also.The answer may get very big so report Mod 1000000007. ( followed by a '\n' ). Constraints: 2 <= N <= 1000 1 <= Q <= 10000 1 <= A[i] <=100 Mod value M = 1000000007 Sample Case1: Input: 4 1234 2 03 25 Output: 22 28\n Explanation: Initially A = {1,2,3,4} ; top value = 20 set A[0] = 3 --> A = {3,2,3,4} ; top value = 22 set A[2] = 5 --> A = {3,2,5,4} ; top value = 28

You might also like