Sum of all elements up to Nth row in a Pascal triangle
Given a row number n, the task is to calculate the sum of all elements of each row up to nth row.
Examples:
Input : 2
Output : 7
Explanation: row 0 have element 1
row 1 have elements 1, 1
row 2 have elements 1, 2, 1
so, sum will be ((1) + (1 + 1) + (1 + 2 + 1)) = 7
Input : 4
Output : 31
Explanation: row 0 have element 1
row 1 have elements 1, 1
row 2 have elements 1, 2, 1
row 3 have elements 1, 3, 3, 1
row 4 have elements 1, 4, 6, 4, 1
so, sum will be ((1) + (1 + 1) + (1 + 2 + 1)
+ (1 + 3 + 3 + 1) + (1 + 4 + 6 + 4 + 1)) = 31
Below is the example of Pascal triangle having 11 rows:
Pascal's triangle
0th row 1
1st row 1 1
2nd row 1 2 1
3rd row 1 3 3 1
4th row 1 4 6 4 1
5th row 1 5 10 10 5 1
6th row 1 6 15 20 15 6 1
7th row 1 7 21 35 35 21 7 1
8th row 1 8 28 56 70 56 28 8 1
9th row 1 9 36 84 126 126 84 36 9 1
10th row 1 10 45 120 210 256 210 120 45 10 1
Naive Approach: In a Pascal triangle, each entry of a row is value of binomial coefficient. So a simple solution is to generating all row elements up to nth row and adding them. But this approach will have O(n3) time complexity. However, it can be optimized up to O(n2) time complexity. Refer the following article to generate elements of Pascal’s triangle:
Better Solution: Let’s have a look on pascal’s triangle pattern
sum of elements in ith row
0th row 1 1 -> 20
1st row 1 1 2 -> 21
2nd row 1 2 1 4 -> 22
3rd row 1 3 3 1 8 -> 23
4th row 1 4 6 4 1 16 -> 24
5th row 1 5 10 10 5 1 32 -> 25
6th row 1 6 15 20 15 6 1 64 -> 26
7th row 1 7 21 35 35 21 7 1 128 -> 27
8th row 1 8 28 56 70 56 28 8 1 256 -> 28
9th row 1 9 36 84 126 126 84 36 9 1 512 -> 29
10th row 1 10 45 120 210 256 210 120 45 10 1 1024 -> 210
As shown above, the sum of elements in the ith row is equal to 2i. Now it can be easily calculated the sum of all elements up to nth row by adding powers of 2.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int calculateSum( int n)
{
long long int sum = 0;
for ( int row = 0; row < n; row++) {
sum = sum + (1 << row);
}
return sum;
}
int main()
{
int n = 10;
cout << " Sum of all elements:" << calculateSum(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long calculateSum( int n)
{
long sum = 0 ;
for ( int row = 0 ; row < n; row++) {
sum = sum + ( 1 << row);
}
return sum;
}
public static void main(String[] args)
{
int n = 10 ;
System.out.println( "Sum of all elements:"
+ calculateSum(n));
}
}
|
Python3
def calculateSum(n) :
sum = 0
for row in range (n):
sum = sum + ( 1 << row)
return sum
n = 10
print ( "Sum of all elements:" , calculateSum(n))
|
C#
using System;
public class GFG {
static long calculateSum( int n)
{
long sum = 0;
for ( int row = 0; row < n; row++) {
sum = sum + (1 << row);
}
return sum;
}
static public void Main()
{
int n = 10;
Console.WriteLine( "Sum of all elements:"
+ calculateSum(n));
}
}
|
PHP
<?php
function calculateSum( $n )
{
$sum = 0;
for ( $row = 0; $row < $n ; $row ++)
{
$sum = $sum + (1 << $row );
}
return $sum ;
}
$n = 10;
echo " Sum of all elements : " .
calculateSum( $n );
?>
|
Javascript
<script>
function calculateSum(n)
{
let sum = 0;
for (let row = 0; row < n; row++)
{
sum = sum + (1 << row);
}
return sum;
}
let n = 10;
document.write( " Sum of all elements : " + calculateSum(n));
</script>
|
Output:
Sum of all elements:1023
Time Complexity: O(n)
Auxiliary Space: O(1) as it is using constant space for variables
Efficient solution:
2n can be expressed as
2n = ( 20 + 21 + 22 + 23 +. . . + 2(n-1) ) + 1
For Example:
26 = ( 20 + 21 + 22 + 23 + 24 + 25 ) + 1
64 = ( 1 + 2 + 4 + 8 +16 + 32 ) + 1
64 = 63 + 1
So, calculate 2n instead of calculating every power of 2 up to (n – 1) and from above example the sum of the power of 2 up to (n – 1) will be (2n – 1).
C++
#include <bits/stdc++.h>
using namespace std;
long long int calculateSum( int n)
{
long long int sum = 0;
sum = 1 << n;
return (sum - 1);
}
int main()
{
int n = 10;
cout << " Sum of all elements:" << calculateSum(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long calculateSum( int n)
{
long sum = 0 ;
sum = 1 << n;
return (sum - 1 );
}
public static void main(String[] args)
{
int n = 10 ;
System.out.println( "Sum of all elements:"
+ calculateSum(n));
}
}
|
Python3
def calculateSum(n) :
sum = 0
sum = 1 << n;
return ( sum - 1 )
n = 10
print ( "Sum of all elements:" , calculateSum(n))
|
C#
using System;
public class GFG {
static long calculateSum( int n)
{
long sum = 0;
sum = 1 << n;
return (sum - 1);
}
static public void Main()
{
int n = 10;
Console.WriteLine( "Sum of all elements:"
+ calculateSum(n));
}
}
|
PHP
<?php
function calculateSum( $n )
{
$sum = 0;
$sum = 1 << $n ;
return ( $sum - 1);
}
$n = 10;
echo " Sum of all elements:" ,
calculateSum( $n );
?>
|
Javascript
function calculateSum(n)
{
sum = 0;
sum = 1 << n;
return (sum - 1);
}
let n = 10;
document.write( " Sum of all elements:" + calculateSum(n));
|
Output:
Sum of all elements:1023
Time Complexity: O(1)
Auxiliary Space: O(1) because constant space has been used
Last Updated :
15 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...