Sunday, 18 January 2015

Pascal's Triangle - Sum of Selected Coefficients

You must have heard of Pascal’s triangle. In this programming assignment, your program is required to compute the sum of a given set of elements of  the Triangle.  Most importantly, your program should have a recursive function to calculate the values in the triangle.

As you know, the values in the Pascal’s Triangle are called Binomial Coefficients.   At the top tip of the arrangement is number 1, which makes up the zeroth row. The first row contains two 1's . In general, in each row, the first and the last numbers are 1.  To find any number in the i th row, add the two numbers immediately above it in (i-1)th row, as shown in the diagram below.



NPUT & OUTPUT:
The input consists of multiple lines. The first line contains a number n which indicates that the number of rows in the Pascal’s triangle will be n+1 (Note that rows of Pascal’s triangle are indexed starting with 0 at the top and the elements in a row are also indexed starting with a 0).
The second line contains a number m which indicates the number of transactions to be performed on the Pascal’s triangle. Each transaction is given in a separate line.  A transaction is a space separated list of integers. The first integer in each list indicates the row number, say R, and the rest of the integers in the list indicate the indices of values in row R. For each transaction, you have to compute the sum of given coefficients in the given row R.

Tuesday, 28 October 2014

Collatz function

Difficulty: Easy

The Collatz function is defined for a positive integer n as follows.
f(n) = 3n+1 if n is odd
        n/2     if n is even

We consider the repeated application of the Collatz function starting with a given integer n, as follows:
f(n), f(f(n)), f(f(f(n))), …

It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260  [Wikipedia: Collatz Conjecture].

Sunday, 19 October 2014

Count occurences of pattern string

Level: Medium

Given a source string S and a pattern string P, count the number of times the pattern string P occurs in the source string S. 
Note: Overlapping sequences are counted as separate occurrences. 

Input Format:
First line is the source string S s.t. 1 <= |S| <= 8192 characters
Second line is the pattern string P s.t. 1 <= |P| <= 8192 characters

Output Format:
Output a single integer containing the number of occurrences of pattern string P in source string S.

Print Subarray

Level: Easy

Given an input character array of A and start index S and end index E, write a function that prints the sub array starting from S (including S) and ending at index E (including E). The character array may contain spaces and tabs.
Note: You are given the main function. Just write the subroutine 'void printSubarray(char *a, int start, int end)'. 

Input Format:
First line is the input array A s.t. 1 <= |A| <= 8192
Second line is the start index S s.t. 0 <= S <= |A|-1
Third line is the ending index E s.t. 0 <= E <= |A|-1

Center Align Text

Level: Easy

Given an input string S, center justify the string by using the character '_' to align the string. There will be neither preceding spaces before the string S nor suffix spaces after the string S. The output should be center-justified in a line of width 64 characters, followed by a newline. 
Note:
1. If S has an odd number of characters then the number of preceding '_' should be one more than the number of trailing '_'
2. If S has an even number of characters then the number of preceding '_' should be equal to the number of trailing '_'

Input Format:
First line is the input string S s.t. 1 <= |S| <= 64