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.
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

Examples:

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to calculate sum of
// all the numbers present at given
// level in an Modified Pascal’s triangle
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate sum
void ans(int n)
{
    if (n == 1)
        cout << "1";
    else
        cout << "0";
}
  
// Driver Code
int main()
{
    int n = 2;
    ans(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to calculate sum of
// all the numbers present at given
// level in an Modified Pascal's triangle
class GFG 
{
  
// Function to calculate sum
static void ans(int n)
{
    if (n == 1)
        System.out.println("1");
    else
        System.out.println("0");
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 2;
    ans(n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to calculate sum of 
# all the numbers present at given 
# level in an Modified Pascal’s triangle 
  
# Function to calculate sum 
def ans(n) : 
  
    if (n == 1) :
        print("1",end=""); 
    else :
        print("0",end=""); 
  
# Driver Code 
if __name__ == "__main__"
  
    n = 2
    ans(n); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to calculate sum of
// all the numbers present at given
// level in an Modified Pascal's triangle
using System;
      
class GFG 
{
  
// Function to calculate sum
static void ans(int n)
{
    if (n == 1)
        Console.WriteLine("1");
    else
        Console.WriteLine("0");
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 2;
    ans(n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

0

Time Complexity: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar, AnkitRai01

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.