Given a level N, the task is to find the sum of all the integers present at this given level in an Alternating Pascal’s triangle.
An Modified Pascal triangle with 5 levels is as shown below.
1 -1 1 1 -2 1 -1 3 -3 1 1 -4 6 -4 1
Input: N = 1 Output: 1 Input: N = 2 Output: 0
Approach: As we can observe that for even level sum is 0 and for odd level except for 1 sum is also 0. So There can be at most 2 cases:
- If L = 1, then answer is 1.
- Otherwise the answer will always be 0.
Below is the implementation of the above approach:
Time Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.