Sum of all the numbers present at given level in Modified Pascal’s triangle
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.
A Modified Pascal triangle with 5 levels is shown below.
1
-1 1
1 -2 1
-1 3 -3 1
1 -4 6 -4 1
Examples:
Input: N = 1
Output: 1
Input: N = 2
Output: 0
Approach: As we can observe 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 the answer is 1.
- Otherwise, the answer will always be 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ans( int n)
{
if (n == 1)
cout << "1" ;
else
cout << "0" ;
}
int main()
{
int n = 2;
ans(n);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static void ans( int n)
{
if (n == 1 )
System.out.println( "1" );
else
System.out.println( "0" );
}
public static void main(String[] args)
{
int n = 2 ;
ans(n);
}
}
|
Python3
def ans(n) :
if (n = = 1 ) :
print ( "1" ,end = "");
else :
print ( "0" ,end = "");
if __name__ = = "__main__" :
n = 2 ;
ans(n);
|
C#
using System;
class GFG
{
static void ans( int n)
{
if (n == 1)
Console.WriteLine( "1" );
else
Console.WriteLine( "0" );
}
public static void Main(String[] args)
{
int n = 2;
ans(n);
}
}
|
Javascript
<script>
function ans(n)
{
if (n == 1)
document.write( "1" );
else
document.write( "0" );
}
var n = 2;
ans(n);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
08 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...