Find the Nth row in Pascal's Triangle (original) (raw)

Last Updated : 15 Jul, 2025

Given a non-negative integer **n, the task is to find the **n th row of Pascal's Triangle.

**Note: The row index starts from 1.

**Examples:

**Input: n = 4
**Output: 1 3 3 1
**Explanation: The elements in the 4th row are 1 3 3 1 as shown in Pascal Triangle.

Nth-row-in-Pascals-Triangle

**Input: n = 1
**Output: 1
**Explanation: The elements in the 1th row is 1 as shown in Pascal Triangle.

Try It Yourselfredirect icon

Table of Content

[Naive Approach] Using Recursion - O(2^n) Time and O(n) Space

The idea is to use the property that each element **i in the Pascal triangle is equal to the sum of two elements directly above it (i-1 and i), with the base condition that the first and last element of any row will be equal to 1.

C++ `

// C++ program to Find the nth row in // Pascal’s Triangle using Recursion #include <bits/stdc++.h> using namespace std;

// Function which recursively finds the // ith value of nth row. int findVal(int i, int n) {

// First and last value of each 
// row will always be 1.
if (i == 1 || i == n) {
    return 1;
}

// Find the (i-1)th and ith 
// value of previous row 
return findVal(i-1, n-1) + findVal(i, n-1);

}

// Function to find the elements // of n'th row in Pascal's Triangle vector nthRowOfPascalTriangle(int n) { vector res;

for (int i=1; i<=n; i++) {
    int val = findVal(i, n);
    res.push_back(val);
}

return res;

}

int main() {

int n = 4; 
vector<int> ans = nthRowOfPascalTriangle(n); 

for(int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
} 
cout << endl;
return 0;

}

Java

// Java program to Find the nth row in // Pascal’s Triangle using Recursion import java.util.*;

class GfG {

// Function which recursively finds the 
// ith value of nth row.
static int findVal(int i, int n) {
    
    // First and last value of each 
    // row will always be 1.
    if (i == 1 || i == n) {
        return 1;
    }
    
    // Find the (i-1)th and ith 
    // value of previous row 
    return findVal(i - 1, n - 1) + findVal(i, n - 1);
}

// Function to find the elements 
// of n'th row in Pascal's Triangle 
static ArrayList<Integer> nthRowOfPascalTriangle(int n) { 
    ArrayList<Integer> res = new ArrayList<>();

    for (int i = 1; i <= n; i++) {
        int val = findVal(i, n);
        res.add(val);
    }

    return res;
} 

public static void main(String[] args) {
    int n = 4; 
    ArrayList<Integer> ans = nthRowOfPascalTriangle(n); 

    for (int i = 0; i < ans.size(); i++) {
        System.out.print(ans.get(i) + " ");
    } 
    System.out.println();
}

}

Python

Python program to Find the nth row in

Pascal’s Triangle using Recursion

Function which recursively finds the

ith value of nth row.

def findVal(i, n):

# First and last value of each 
# row will always be 1.
if i == 1 or i == n:
    return 1

# Find the (i-1)th and ith 
# value of previous row 
return findVal(i - 1, n - 1) + findVal(i, n - 1)

Function to find the elements

of n'th row in Pascal's Triangle

def nthRowOfPascalTriangle(n): res = []

for i in range(1, n + 1):
    val = findVal(i, n)
    res.append(val)

return res

if name == "main": n = 4 ans = nthRowOfPascalTriangle(n)

for i in range(len(ans)):
    print(ans[i], end=" ")
print()

C#

// C# program to Find the nth row in // Pascal’s Triangle using Recursion using System; using System.Collections.Generic;

class GfG {

// Function which recursively finds the 
// ith value of nth row.
static int findVal(int i, int n) {
    
    // First and last value of each 
    // row will always be 1.
    if (i == 1 || i == n) {
        return 1;
    }
    
    // Find the (i-1)th and ith 
    // value of previous row 
    return findVal(i - 1, n - 1) + findVal(i, n - 1);
}

// Function to find the elements 
// of n'th row in Pascal's Triangle 
static List<int> nthRowOfPascalTriangle(int n) { 
    List<int> res = new List<int>();

    for (int i = 1; i <= n; i++) {
        int val = findVal(i, n);
        res.Add(val);
    }

    return res;
} 

static void Main(string[] args) {
    int n = 4; 
    List<int> ans = nthRowOfPascalTriangle(n); 

    for (int i = 0; i < ans.Count; i++) {
        Console.Write(ans[i] + " ");
    } 
    Console.WriteLine();
}

}

JavaScript

// Function which recursively finds the // ith value of nth row. function findVal(i, n) {

// First and last value of each 
// row will always be 1.
if (i === 1 || i === n) {
    return 1;
}

// Find the (i-1)th and ith 
// value of previous row 
return findVal(i - 1, n - 1) + findVal(i, n - 1);

}

// Function to find the elements // of n'th row in Pascal's Triangle function nthRowOfPascalTriangle(n) { let res = [];

for (let i = 1; i <= n; i++) {
    let val = findVal(i, n);
    res.push(val);
}

return res;

}

// Driver code let n = 4; let ans = nthRowOfPascalTriangle(n); console.log(ans.join(' '));

`

**[Better Approach] Using Recursion Level by Level - O(n^2) Time and O(n) Space

The idea is to recursively find the **n'th **row by recursively finding the previous row. Then use the previous row to calculate the values of **current row.

C++ `

// C++ program to Find the nth row in // Pascal’s Triangle using Recursion #include <bits/stdc++.h> using namespace std;

// Function to find the elements // of n'th row in Pascal's Triangle vector nthRowOfPascalTriangle(int n) { vector curr;

// 1st element of every row is 1 
curr.push_back(1); 

// Check if the row that has to 
// be returned is the first row 
if (n == 1) { 
    return curr; 
} 

// Generate the previous row 
vector<int> prev = nthRowOfPascalTriangle(n - 1); 

for(int i = 1; i < prev.size(); i++) {
    
    // Generate the elements of the current
    // row with the help of the previous row 
    int val = prev[i - 1] + prev[i]; 
    curr.push_back(val); 
} 
curr.push_back(1); 

// Return the row 
return curr; 

}

int main() {

int n = 4; 
vector<int> ans = nthRowOfPascalTriangle(n); 

for(int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
} 
cout << endl;
return 0;

}

Java

// Java program to Find the nth row in // Pascal’s Triangle using Recursion import java.util.*;

class GfG {

// Function to find the elements 
// of n'th row in Pascal's Triangle 
static ArrayList<Integer> nthRowOfPascalTriangle(int n) {
    ArrayList<Integer> curr = new ArrayList<>();

    // 1st element of every row is 1 
    curr.add(1);

    // Check if the row that has to 
    // be returned is the first row 
    if (n == 1) {
        return curr;
    }

    // Generate the previous row 
    ArrayList<Integer> prev = nthRowOfPascalTriangle(n - 1);

    for (int i = 1; i < prev.size(); i++) {

        // Generate the elements of the current
        // row with the help of the previous row 
        int val = prev.get(i - 1) + prev.get(i);
        curr.add(val);
    }
    curr.add(1);

    // Return the row 
    return curr;
}

public static void main(String[] args) {
    int n = 4;
    ArrayList<Integer> ans = nthRowOfPascalTriangle(n);

    for (int i = 0; i < ans.size(); i++) {
        System.out.print(ans.get(i) + " ");
    }
    System.out.println();
}

}

Python

Python program to Find the nth row in

Pascal’s Triangle using Recursion

Function to find the elements

of n'th row in Pascal's Triangle

def nthRowOfPascalTriangle(n): curr = []

# 1st element of every row is 1 
curr.append(1)

# Check if the row that has to 
# be returned is the first row 
if n == 1:
    return curr

# Generate the previous row 
prev = nthRowOfPascalTriangle(n - 1)

for i in range(1, len(prev)):

    # Generate the elements of the current
    # row with the help of the previous row 
    val = prev[i - 1] + prev[i]
    curr.append(val)

curr.append(1)

# Return the row 
return curr

if name == "main": n = 4 ans = nthRowOfPascalTriangle(n)

for val in ans:
    print(val, end=" ")
print()

C#

// C# program to Find the nth row in // Pascal’s Triangle using Recursion using System; using System.Collections.Generic;

class GfG {

// Function to find the elements 
// of n'th row in Pascal's Triangle 
static List<int> nthRowOfPascalTriangle(int n) {
    List<int> curr = new List<int>();

    // 1st element of every row is 1 
    curr.Add(1);

    // Check if the row that has to 
    // be returned is the first row 
    if (n == 1) {
        return curr;
    }

    // Generate the previous row 
    List<int> prev = nthRowOfPascalTriangle(n - 1);

    for (int i = 1; i < prev.Count; i++) {

        // Generate the elements of the current
        // row with the help of the previous row 
        int val = prev[i - 1] + prev[i];
        curr.Add(val);
    }
    curr.Add(1);

    // Return the row 
    return curr;
}

static void Main(string[] args) {
    int n = 4;
    List<int> ans = nthRowOfPascalTriangle(n);

    for (int i = 0; i < ans.Count; i++) {
        Console.Write(ans[i] + " ");
    }
    Console.WriteLine();
}

}

JavaScript

// JavaScript program to Find the nth row in // Pascal’s Triangle using Recursion

// Function to find the elements // of n'th row in Pascal's Triangle function nthRowOfPascalTriangle(n) { let curr = [];

// 1st element of every row is 1 
curr.push(1);

// Check if the row that has to 
// be returned is the first row 
if (n === 1) {
    return curr;
}

// Generate the previous row 
let prev = nthRowOfPascalTriangle(n - 1);

for (let i = 1; i < prev.length; i++) {

    // Generate the elements of the current
    // row with the help of the previous row 
    let val = prev[i - 1] + prev[i];
    curr.push(val);
}
curr.push(1);

// Return the row 
return curr;

}

let n = 4; let ans = nthRowOfPascalTriangle(n); console.log(ans.join(' '));

`

**[Expected Approach] Using Combinatorics - O(n) Time and O(n) Space

The idea is to use the mathematical relationship between consecutive elements in a Pascal's Triangle row, rather than computing the entire triangle. Since the **nth row consists of binomial coefficients **nC0, nC1, ..., nCn, we can calculate each element directly from the previous one using the formula: nCr = (nCr-1 * (n-r+1))/r. This approach starts with nC0 = 1 and efficiently computes each subsequent value

**Note: This approach uses 0-based indexing because the mathematical formula used for calculating binomial coefficients aligns with 0-based indexing.

pascal-s-triangle

Step by step approach:

  1. Start with first element of the row as 1 (nC0 = 1).
  2. For each subsequent element, use the formula nCr = (nCr-1 * (n-r+1))/r.
  3. Build the row progressively by calculating one element at a time.
  4. Adjust for 1-based indexing by decrementing n before calculations.

**How is the formula calculated?

C++ `

// C++ program to Find the nth row in Pascal’s // Triangle using efficient approach #include <bits/stdc++.h> using namespace std;

// Function to find the elements // of n'th row in Pascal's Triangle vector nthRowOfPascalTriangle(int n) {

// To follow 0 based indexing, decrement n 
n--;

vector<int> res;

// nC0 = 1
int prev = 1;
res.push_back(prev);

for (int i = 1; i <= n; i++) {
  
    // nCr = (nCr-1 * (n - r + 1))/r 
    int curr = (prev * (n - i + 1)) / i;
    res.push_back(curr);
    prev = curr;
}
return res;

}

int main() {

int n = 4; 
vector<int> ans = nthRowOfPascalTriangle(n); 

for(int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
} 
cout << endl;
return 0;

}

Java

// Java program to Find the nth row in Pascal’s // Triangle using efficient approach import java.util.*;

class GfG {

// Function to find the elements 
// of n'th row in Pascal's Triangle 
static ArrayList<Integer> nthRowOfPascalTriangle(int n) {

    // To follow 0 based indexing, decrement n 
    n--;

    ArrayList<Integer> res = new ArrayList<>();

    // nC0 = 1
    int prev = 1;
    res.add(prev);

    for (int i = 1; i <= n; i++) {

        // nCr = (nCr-1 * (n - r + 1))/r 
        int curr = (prev * (n - i + 1)) / i;
        res.add(curr);
        prev = curr;
    }
    return res;
}

public static void main(String[] args) {
    int n = 4;
    ArrayList<Integer> ans = nthRowOfPascalTriangle(n);

    for (int i = 0; i < ans.size(); i++) {
        System.out.print(ans.get(i) + " ");
    }
    System.out.println();
}

}

Python

Python program to Find the nth row in Pascal’s

Triangle using efficient approach

Function to find the elements

of n'th row in Pascal's Triangle

def nthRowOfPascalTriangle(n):

# To follow 0 based indexing, decrement n 
n -= 1

res = []

# nC0 = 1
prev = 1
res.append(prev)

for i in range(1, n + 1):

    # nCr = (nCr-1 * (n - r + 1))/r 
    curr = (prev * (n - i + 1)) // i
    res.append(curr)
    prev = curr

return res

if name == "main": n = 4 ans = nthRowOfPascalTriangle(n)

for val in ans:
    print(val, end=" ")
print()

C#

// C# program to Find the nth row in Pascal’s // Triangle using efficient approach using System; using System.Collections.Generic;

class GfG {

// Function to find the elements 
// of n'th row in Pascal's Triangle 
static List<int> nthRowOfPascalTriangle(int n) {

    // To follow 0 based indexing, decrement n 
    n--;

    List<int> res = new List<int>();

    // nC0 = 1
    int prev = 1;
    res.Add(prev);

    for (int i = 1; i <= n; i++) {

        // nCr = (nCr-1 * (n - r + 1))/r 
        int curr = (prev * (n - i + 1)) / i;
        res.Add(curr);
        prev = curr;
    }
    return res;
}

static void Main(string[] args) {
    int n = 4;
    List<int> ans = nthRowOfPascalTriangle(n);

    for (int i = 0; i < ans.Count; i++) {
        Console.Write(ans[i] + " ");
    }
    Console.WriteLine();
}

}

JavaScript

// Function to find the elements // of n'th row in Pascal's Triangle function nthRowOfPascalTriangle(n) {

// To follow 0 based indexing, decrement n 
n--;

let res = [];

// nC0 = 1
let prev = 1;
res.push(prev);

for (let i = 1; i <= n; i++) {

    // nCr = (nCr-1 * (n - r + 1))/r 
    let curr = Math.floor((prev * (n - i + 1)) / i);
    res.push(curr);
    prev = curr;
}
return res;

}

let n = 4; let ans = nthRowOfPascalTriangle(n); console.log(ans.join(' '));

`