// Cpp program to Find the nth row in // Pascal’s Triangle using Recursion #include…
- User
// Cpp 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 rowIndex in Pascal's Triangle vector getRow(int rowIndex) { vector cur;
// 1st element of every row is 1 cur.push_back(1); // Check if the row that has to // be returned is the first row if (rowIndex == 0) { return cur; } // Generate the previous row vector<int> prev = getRow(rowIndex - 1); for(int i = 1; i < prev.size(); i++) { // Generate the elements of the current // row by the help of the previous row int curr = prev[i - 1] + prev[i]; cur.push_back(curr); } cur.push_back(1); // Return the row return cur;}
int main() { int n = 3; vector 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;}
This is the formatting used by GfG now I want you to convert this code into this format //recursion class Solution { private: int fun(int ind,int prev,int n,vector& nums){ if(ind==n) return 0;
int len= 0+fun(ind+1,prev,n,nums); //not take if(prev ==-1 || nums[ind]>nums[prev]){ //take len= max(len,1+ fun(ind+1,ind,n,nums)); } return = len; }public: int lengthOfLIS(vector& nums) { int n= nums.size(); return fun(0,-1,n,nums); } }; Guidelines (Variable names)
Single Array arr[] Multiple Arrays a[], b[], c[] Single String “s” Multiple String “s1”, “s2” String Pattern Matching txt for text and pat for pattern Target target K elements k Array Size n 2D Array mat[][], arr[][] Multiple 2D Arrays a[][], b[][], c[][] Vertices V Edges E Weight wt Adjacency List adj[[]] Adjacency Matrix mat[][] Edges edges[] Source src Linked List Linked List Head of Linked List head Doubly Linked List Doubly Linked List Circular Linked List Circular Linked List Key key root root Multiple roots r1, r2, r3
Function Name Keep it simple, Avoid underscore in variables, use camelCase instead, ex-tempArr
Formatting Use [ ] (first “[“ bracket in arrays, 2d array, grid, pair) in input and output Example [1, 2, 3] 2D array -> [[1,2], [3,5]]
Use “ ” (Quotation marks for String) Ex: “GeeksforGeeks”
Use ‘ ’ (Apostrophe for characters) Ex: ‘c’
Considerations Add a minimum of three examples General Case Edge Case Special Case Please do chatGPT
Boolean return type, print “true” & “false”
Keep variable ordering the same across Problem statement Examples Function Parameters Constraints Custom Input/Test Cases Check brut force(ref. Gfg article, ChatGPT)
Use pass-by-reference in array & string in C++ Use ArrayList in return type while Array in Parameters in Java
To Avoid Long Long(exception for special cases) Modulo
In case of returning empty arrays and string [] -> array, matrix "" -> empty string
this is the General variable naming
- Assistant
Sure! Below is the converted C++ code for finding the nth row in Pascal’s Triangle using recursion, formatted according to your specified guidelines. The code is encapsulated within a
Solutionclass, following proper variable naming conventions and formatting styles.//recursion class Solution { private: // Helper function to generate the nth row of Pascal's Triangle vector<int> generateRow(int n) { vector<int> arr; // First element of every row is 1 arr.push_back(1); // Base case: if n is 0, return the first row if (n == 0) { return arr; } // Recursively generate the previous row vector<int> prev = generateRow(n - 1); // Generate the current row using the previous row for(int i = 1; i < prev.size(); i++) { int curr = prev[i - 1] + prev[i]; arr.push_back(curr); } // Last element of every row is 1 arr.push_back(1); return arr; } public: // Public function to get the nth row of Pascal's Triangle vector<int> getRow(int n) { return generateRow(n); } };Explanation
-
Class Structure: The code is organized within a
Solutionclass, which is a common practice in platforms like LeetCode. -
Private Helper Function (
generateRow):- Purpose: Recursively generates the nth row of Pascal's Triangle.
- Base Case: If
nis0, it returns a vector containing only1. - Recursive Case: It first generates the
(n-1)throw and then constructs the current row by summing adjacent elements from the previous row.
-
Public Function (
getRow):- Purpose: Serves as the main function to be called, which internally uses the helper function to generate the desired row.
-
Variable Naming:
- Uses
arrfor the current row andprevfor the previous row, adhering to the naming conventions.
- Uses
-
Formatting:
- Proper indentation and spacing are maintained for readability.
- Comments are added to explain each significant step.
Examples
Example 1
Input:
int n = 3;Output:
[1, 3, 3, 1]Example 2
Input:
int n = 0;Output:
[1]Example 3
Input:
int n = 5;Output:
[1, 5, 10, 10, 5, 1]Usage
To use the above
Solutionclass, you can create an instance of the class and call thegetRowmethod with the desired row index. Here's how you can do it:#include <bits/stdc++.h> using namespace std; // Include the Solution class here int main() { Solution solution; int n = 3; vector<int> pascalRow = solution.getRow(n); // Print the result cout << "["; for(int i = 0; i < pascalRow.size(); i++) { if (i == pascalRow.size() - 1) cout << pascalRow[i]; else cout << pascalRow[i] << ", "; } cout << "]" << endl; return 0; }Output:
[1, 3, 3, 1]This setup ensures that the code is clean, readable, and adheres to the specified guidelines for variable naming and formatting.
-