Pascal’s Triangle II In C,CPP,JAVA,PYTHON,C#,JS
Problem Statement: Pascal’s Triangle II Given an integer rowIndex, return the rowIndex-th row of Pascal’s Triangle. The rowIndex is 0-based, so the first row is row 0. For example: Approach Code Implementations C: #include <stdio.h>#include <stdlib.h>void generateRow(int rowIndex) { int *row = (int *)malloc((rowIndex + 1) * sizeof(int)); row[0] = 1; // First element is always 1 for (int i = 1; i <= rowIndex; i++) { // Update the row in reverse to avoid overwriting previous values for (int j = i; j > 0; j–) { row[j] = row[j] + row[j – 1]; } } // Print the row for (int i = 0; i <= rowIndex; i++) { printf(“%d “, row[i]); } printf(“\n”); free(row);}int main() { int rowIndex = 3; generateRow(rowIndex); // Output: 1 3 3 1 return 0;} C++: #include <iostream>#include <vector>using namespace std;vector<int> getRow(int rowIndex) { vector<int> row(rowIndex + 1, 1); // Initialize the row with 1’s for (int i = 1; i <= rowIndex; i++) { // Update the row in reverse order to prevent overwriting for (int j = i – 1; j > 0; j–) { row[j] += row[j – 1]; } } return row;}int main() { int rowIndex = 3; vector<int> row = getRow(rowIndex); for (int num : row) { cout << num << ” “; } cout << endl; // Output: 1 3 3 1 return 0;} Java: import java.util.ArrayList;import java.util.List;public class PascalsTriangleII { public static List<Integer> getRow(int rowIndex) { List<Integer> row = new ArrayList<>(); row.add(1); // First element is always 1 for (int i = 1; i <= rowIndex; i++) { // Update the row in reverse order to avoid overwriting for (int j = i – 1; j > 0; j–) { row.set(j, row.get(j) + row.get(j – 1)); } row.add(1); // Add the last element of the row (which is always 1) } return row; } public static void main(String[] args) { int rowIndex = 3; List<Integer> row = getRow(rowIndex); for (int num : row) { System.out.print(num + ” “); } System.out.println(); // Output: 1 3 3 1 }} Python: def getRow(rowIndex): row = [1] # First element is always 1 for i in range(1, rowIndex + 1): # Update the row in reverse order to avoid overwriting for j in range(i – 1, 0, -1): row[j] += row[j – 1] row.append(1) # Add the last element of the row (which is always 1) return row# Example usagerowIndex = 3print(getRow(rowIndex)) # Output: [1, 3, 3, 1] C#: using System;using System.Collections.Generic;class PascalsTriangleII { public static List<int> GetRow(int rowIndex) { var row = new List<int> { 1 }; // First element is always 1 for (int i = 1; i <= rowIndex; i++) { // Update the row in reverse order to prevent overwriting for (int j = i – 1; j > 0; j–) { row[j] += row[j – 1]; } row.Add(1); // Add the last element of the row (which is always 1) } return row; } static void Main() { int rowIndex = 3; var row = GetRow(rowIndex); foreach (var num in row) { Console.Write(num + ” “); } Console.WriteLine(); // Output: 1 3 3 1 }} JavaScript: function getRow(rowIndex) { let row = [1]; // First element is always 1 for (let i = 1; i <= rowIndex; i++) { // Update the row in reverse order to avoid overwriting for (let j = i – 1; j > 0; j–) { row[j] += row[j – 1]; } row.push(1); // Add the last element of the row (which is always 1) } return row;}// Example usagelet rowIndex = 3;console.log(getRow(rowIndex)); // Output: [1, 3, 3, 1] Explanation: For rowIndex = 3, the steps to generate the row [1, 3, 3, 1] are: Time Complexity: Space Complexity: Summary: This solution computes the rowIndex-th row of Pascal’s Triangle directly using an efficient iterative approach, making it more space- and time-efficient than generating the entire triangle.
Pascal’s Triangle II In C,CPP,JAVA,PYTHON,C#,JS Read More »