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.