Check if Pascal’s Triangle is possible with a complete layer by using numbers upto N
Given a number N, the task is to determine if it is possible to make Pascal’s triangle with a complete layer by using total number N integer if possible print Yes otherwise print No.
Note: Pascal’s triangle is a triangular array of the binomial coefficients. Following are the first 6 rows of Pascal’s Triangle.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
In Pascal’s Triangle from the topmost layer there is 1 integer, at every next layer from top to bottom size of the layer increased by 1.
Input: N = 10
You can use 1, 2, 3 and 4 integers to make first, second, third, and fourth layer of pascal’s triangle respectively and also N = 10 satisfy by using (1 + 2 + 3 + 4) integers on each layer = 10.
Input: N = 5
You can use 1 and 2 integers to make first and second layer respectively and after that you have only 2 integers left and you can’t make 3rd layer complete as that layer required 3 integers.
Approach: Here we are using integer 1, 2, 3, … on every layer starting from first layer, so we can only make Pascal’s triangle complete if it’s possible to represent N by the sum of 1 + 2 +…
- The sum of first X integers is given by
- We can only make pascal’s triangle by using N integers if and only if where X must be a positive integer. So we have to check is there any positive integer value of x exist or not.
- To determine value of X from second step we can deduced the formula as:
- If the value of X integer for the given value of N then we can make Pascal Triangle. Otherwise, we can’t make Pascal Triangle.
Below is the implementation of the above approach:
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)