MobiCastle Interview Question
Software Engineer in TestsCountry: United Kingdom
#include<iostream>
using namespace std;
int main()
{
int no_of_rows, row_counter, pyramid_element = 1, column_counter,row_to_sum, row_sum = 0, before_numbers=0;
cout<<"How many rows of Pyramid you want?";
cin>>no_of_rows;
// Pyramid Construction
for(row_counter = 1; row_counter <= no_of_rows; row_counter++)
{
for(column_counter = 1; column_counter <= row_counter; column_counter++)
{
cout<<pyramid_element<<"\t";
pyramid_element++;
}
cout<<endl;
}
//Sum of row element of a pyramid
cout<<"Which row you want to sum up?";
cin>>row_to_sum;
for(row_counter = (row_to_sum - 1); row_counter >= 1; row_counter--)
{
before_numbers = before_numbers + row_counter;
}
for(row_counter = before_numbers + 1; row_counter <= before_numbers + row_to_sum; row_counter++)
{
row_sum = row_sum + row_counter;
}
cout<<"Sum of "<<row_to_sum<<"is : "<<row_sum;
return 0;
}
We should find out the first element of the row in order to calculate the sum of complete row. Using recursion/iteration we find out the first element of row in O(N) and using sum of integers formula (n*(n+1))/2 we find out the resultant sum in O(1).
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PyramidRowSum {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
long rowStartNum = rowStartNum(N, true);
long prevNumTotal = sumOfN(rowStartNum - 1);
long nextNumTotal = sumOfN(rowStartNum + (N - 1));
System.out.println(nextNumTotal - prevNumTotal);
}
public static long rowStartNum(long N, boolean recursion) {
if (recursion) {
if (N == 1) {
return 1l;
} else {
return rowStartNum(N-1, recursion) + N - 1;
}
} else {
long prevFirstElement = 1;
for (long i = 2; i <= N; i++) {
prevFirstElement = prevFirstElement + (i - 1);
}
return prevFirstElement;
}
}
public static long sumOfN(long N) {
return (N * (N + 1)) / 2l;
}
}
This problem is essentially summing a subarray/part of an array of nums from 1..len(nums). To do this, the only tricky part is to find out the starting index to sum and the how many elements to do so. Luckily, we can easily do this using the triangular numbers formula and we are also given an n. This leads to a simple solution. I present my 4-line solution in Python below.
Elegant Solution (Python):
def getNthRowSum(n):
n = n - 1 # Since we offset by 0
if n == 0:
return 1 # First row
starting_ind = (n*(n+1)//2)+1 # Adding 1 since arrays/lists start at 0
return sum(range(starting_ind, starting_ind+n+1))
Test code:
for i in range(1, 7):
print('%dth row: Sum = %d' % (i, getNthRowSum(i)))
'''
Output:
1th row: Sum = 1
2th row: Sum = 5
3th row: Sum = 15
4th row: Sum = 34
5th row: Sum = 65
6th row: Sum = 111
'''
Let's denote S(N) = row1 + row2 + .... + rowN. Then F(N) = S(N) - S(N-1), when F is the function we have to compute.
On the other hand, S(N) = 1 + 2 + ... + N = N (N+1) / 2 -- it's just aritmetic series.
So the answer could be computed in O(1) time and memory:
de
def S(N):
return N*(N+1)/2
def getPramidRowSum(N):
if N == 1:
return 1
return S(N) - S(N-1)
Give each row an index, and we see that there's a recursion hiding here
0: 1
1: 2=1+1 3=1+2
2: 4=1+3 5=1+4 6
3: 7 8 9 10
4: 11 12 13 14 15
...
The sum uses N integers, starting from the first element of row N, which comes out to be sum of all integers from 1 to N-1, plus 1, which is N*(N-1)/2 + 1.
Ex:
Row 4
Start element = 4*(4-1)/2 + 1 = 6 + 1
7 + 8 + 9 + 10 = 1+6 + 1+7 + 1+8 + 1+9 = (1+1+1+1) + 4*6 + (1+2+3) [Notice the sum from 1 to N-1 comes up again)
so,
temp = N*(N-1) / 2
sum = N + (N + 1) * temp
def Sum(N):
temp = (N * (N-1)) / 2
return N + (N+1) * temp
public static void main(String[] args){
int rowNum = 3;
int firstElement = rowNum*(rowNum-1)/2 + 1;
System.out.println(firstElement);
int sum1 = (firstElement+ rowNum)* (firstElement+ rowNum-1)/ 2;
int sum2 = (firstElement-1)*(firstElement)/2;
int total = sum1-sum2;
System.out.println(total);
}
Answer is simply n*(n²+1) / 2
- vikramambre2401 March 19, 2018