You are on page 1of 3

APS LAB-EXAM 1 (28-09-2011)

Problem A : Alternating Sequence


Given a long binary string S, of n characters ( 0s and 1s, 1 <= n <= 1000 ) Your task is to find the length of the longest alternating sub string. An alternating string is either 01010101... or 101010101...

Input : a string (max length is 1000) Output : length of longest alternating sub string.

Sample Cases 1: Input: 00110101001 Output: 6\n Explanation : it has a sub string "101010" in it.

Sample Cases 2: Input: 000 Output: 1\n Explanation : it has sub string "0" in it.

Sample Cases 3: Input: 0000011111 Ouput: 2\n Explanation: it has substring "01" in it.

Problem B : Find Triplets !!!


Given an array A of N distinct integers, find the number of triplets (i,j,k) such that A[i] = A[j] + A[k] and i!=j, j!=k and k!=i. where 3 <= N <= 5000 -5000 <= A[i] <= 5000

Input : First line contain N size of array(3 <= N <= 5000).next line contain N distinct integer separated by spaces. Output : Print the answer in a single line, followed by a new line ('\n'). Note : '\n' at the end of output in sample cases is just to make sure you print a new line after the answer, no need to display '\n' in output.

Sample case 1: Input: 5 1 2 -2 4 -6 Output: 2\n Explanation : -2 = -6+4, 2 = -2 + 4

Sample Case 2: Input: 5 1 -1 2 4 3 Output: 5\n Explanation: 1 = -1+2, 2 = -1+3, 3=1+2, 3 =-1+4, 4=1+3

Problem C : Water Taps


Did you know that Hyderabad Marathon is on Oct 9 this year. It will be a memorable event to participate, just you have to finish 22kms in 3.5 hours, starting at 5:30am in the morning ;). The organizers are doing the preparations and they want to install water taps at some points on the road. They have identified N suitable points for the water taps to be installed and they need to choose exactly K of these points to install the water taps. Having two water taps very near to each other is not worth it, so your task is to select some K points such that the minimum distance between any two of them is as large as possible.

Input First line contains N K ( 2 <= K <= N <= 100,000 ). N lines follow, each has a position suitable for tap to be installed. You can imagine the road as a straight line ( x-axis ). ( 0 <= xi <= 109 ) Output Print the answer in a single line, followed by a new line ( just a '\n', no need to display ) Time Limit : 1s

Sample Case Input 53 1 2 8 4 9 Output 3\n Explanation We can install the 3 taps at x = 1, 4 and 8. The minimum gap is between 1 and 4 = 4 - 1 = 3. You can't select any other 3 places such that the minimum distance is more than 3.

You might also like