Find the Nth row in Pascal 's Triangle | C ++ program to find the Nth index row in Pascal 's triangle ; Function to find the elements of rowIndex in Pascal 's Triangle ; 1 st element of every row is 1 ; Check if the row that has to be returned is the first row ; Generate the previous row ; Generate the elements of the current row by the help of the previous row ; Return the row ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }