Find the Nth row in Pascal’s Triangle
Given a non-negative integer N, the task is to find the Nth row of Pascal’s Triangle.
Note: The row index starts from 0.
Pascal’s Triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Examples:
Input: N = 3
Output: 1, 3, 3, 1
Explanation:
The elements in the 3rd row are 1 3 3 1.
Input: N = 0
Output: 1
Naive Approach:
The simplest approach to solve the problem is to use Recursion. Find the row of the previous index first using recursion and then calculate the values of the current row with the help of the previous one. Repeat this process up to the Nth row.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > getRow( int rowIndex)
{
vector< int > currow;
currow.push_back(1);
if (rowIndex == 0)
{
return currow;
}
vector< int > prev = getRow(rowIndex - 1);
for ( int i = 1; i < prev.size(); i++)
{
int curr = prev[i - 1] + prev[i];
currow.push_back(curr);
}
currow.push_back(1);
return currow;
}
int main()
{
int n = 3;
vector< int > arr = getRow(n);
for ( int i = 0; i < arr.size(); i++)
{
if (i == arr.size() - 1)
cout << arr[i];
else
cout << arr[i] << ", " ;
}
return 0;
}
|
Java
import java.util.ArrayList;
public class geeks {
public static ArrayList<Integer> getRow(
int rowIndex)
{
ArrayList<Integer> currow
= new ArrayList<Integer>();
currow.add( 1 );
if (rowIndex == 0 ) {
return currow;
}
ArrayList<Integer> prev = getRow(rowIndex
- 1 );
for ( int i = 1 ; i < prev.size(); i++) {
int curr = prev.get(i - 1 )
+ prev.get(i);
currow.add(curr);
}
currow.add( 1 );
return currow;
}
public static void main(String[] args)
{
int n = 3 ;
ArrayList<Integer> arr = getRow(n);
for ( int i = 0 ; i < arr.size(); i++) {
if (i == arr.size() - 1 )
System.out.print(arr.get(i));
else
System.out.print(arr.get(i)
+ ", " );
}
}
}
|
Python3
def getRow(rowIndex) :
currow = []
currow.append( 1 )
if (rowIndex = = 0 ) :
return currow
prev = getRow(rowIndex - 1 )
for i in range ( 1 , len (prev)) :
curr = prev[i - 1 ] + prev[i]
currow.append(curr)
currow.append( 1 )
return currow
n = 3
arr = getRow(n)
for i in range ( len (arr)) :
if (i = = ( len (arr) - 1 )) :
print (arr[i])
else :
print (arr[i] , end = ", " )
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static List< int > getRow( int rowIndex)
{
List< int > currow = new List< int >();
currow.Add(1);
if (rowIndex == 0)
{
return currow;
}
List< int > prev = getRow(rowIndex - 1);
for ( int i = 1; i < prev.Count; i++)
{
int curr = prev[i - 1] + prev[i];
currow.Add(curr);
}
currow.Add(1);
return currow;
}
public static void Main(String[] args)
{
int n = 3;
List< int > arr = getRow(n);
for ( int i = 0; i < arr.Count; i++)
{
if (i == arr.Count - 1)
Console.Write(arr[i]);
else
Console.Write(arr[i] + ", " );
}
}
}
|
Javascript
<script>
function getRow(rowIndex)
{
let currow = [];
currow.push(1);
if (rowIndex == 0)
{
return currow;
}
let prev = getRow(rowIndex - 1);
for (let i = 1; i < prev.length; i++)
{
let curr = prev[i - 1] + prev[i];
currow.push(curr);
}
currow.push(1);
return currow;
}
let n = 3;
let arr = getRow(n);
for (let i = 0; i < arr.length; i++)
{
if (i == arr.length - 1)
document.write(arr[i]);
else
document.write(arr[i] + ", " );
}
</script>
|
Time Complexity: O(N2)
Space Complexity: O(N)
Efficient Approach:
Follow the steps below to optimize the above approach:
- Unlike the above approach, we will just generate only the numbers of the Nth row.
- We can observe that the Nth row of the Pascal’s triangle consists of following sequence:
NC0, NC1, ......, NCN - 1, NCN
- Since, NC0 = 1, the following values of the sequence can be generated by the following equation:
NCr = (NCr - 1 * (N - r + 1)) / r where 1 ? r ? N
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void generateNthrow( int N)
{
int prev = 1;
cout << prev;
for ( int i = 1; i <= N; i++) {
int curr = (prev * (N - i + 1)) / i;
cout << ", " << curr;
prev = curr;
}
}
int main()
{
int N = 5;
generateNthrow(N);
return 0;
}
|
C
#include <stdio.h>
void generateNthrow( int N)
{
int prev = 1;
printf ( "%d" , prev);
for ( int i = 1; i <= N; i++) {
int curr = (prev * (N - i + 1)) / i;
printf ( ",%d " , curr);
prev = curr;
}
}
int main()
{
int N = 5;
generateNthrow(N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void generateNthrow( int N)
{
int prev = 1 ;
System.out.print(prev);
for ( int i = 1 ; i <= N; i++)
{
int curr = (prev * (N - i + 1 )) / i;
System.out.print( ", " + curr);
prev = curr;
}
}
public static void main (String[] args)
{
int N = 5 ;
generateNthrow(N);
}
}
|
Python3
def generateNthRow (N):
prev = 1
print (prev, end = '')
for i in range ( 1 , N + 1 ):
curr = (prev * (N - i + 1 )) / / i
print ( "," , curr, end = '')
prev = curr
N = 5
generateNthRow(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void generateNthrow( int N)
{
int prev = 1;
Console.Write(prev);
for ( int i = 1; i <= N; i++)
{
int curr = (prev * (N - i + 1)) / i;
Console.Write( ", " + curr);
prev = curr;
}
}
public static void Main(String[] args)
{
int N = 5;
generateNthrow(N);
}
}
|
Javascript
<script>
function generateNthrow(N)
{
let prev = 1;
document.write(prev);
for (let i = 1; i <= N; i++)
{
let curr = (prev * (N - i + 1)) / i;
document.write( ", " + curr);
prev = curr;
}
}
let N = 5;
generateNthrow(N);
</script>
|
Output:
1, 5, 10, 10, 5, 1
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
08 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...