I have understood the pattern and everything but the logic for n>5 is an issue.
Can you tell me the logic for code if n>5
Pascal’s triangle is the triangular array of the binomial coefficients.The number of entries in a given line is equal to the line number.Every entry in the line is the value of a binomial coefficient. The value of ith entry in the number line is C(line,i)(i.e apply mathematical combination formula ).
Where C(line,i)=line!/((line-i)!*i!).
Given pattern can be formed using 3 approches:
1.A simple method is to run two loops and calculate the value of binomial coefficient in inner loop. Complexity: O(N3).
2.O(N2) time and O(n2) space complexity.In this method store the previously generated values in 2-D array. Use these values to generate value in a line.
3.O(N2) time and O(1) space complexity. In this method calculate C(line,i) using C(line,i-1). It can be calculated in O(1) time as follows:
C(line,i-1)=line!/((line-i+1)!*(i-1)!)
C(line,i)=line!/((line-i)!*i!)
C(line,i)=C(line,i-1)*(line-i+1)/i.
Here if you look closely every ith number is the binomial coefficient of the expansion (1+x)^k where k starts from 0.
Also number in line k at ith position = k!/( (k-i)! * i!)
and number in k line at (i-1)th position = k!/( (k-i + 1)! * (i-1)! )
Therefore number in line k at ith position is (number in k line at (i-1)th position) * (k - i + 1) / i
@Utkarshsingh5298291,
Use row<n instead of row<=n.
Your code is correct and you have implemented it correctly.
https://ide.codingblocks.com/s/253891 corrected code.
For input 6 you were getting the output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Correct output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
You were printing the extra line. Hence the wrong answer.