Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:40:15+00:00 2026-06-10T05:40:15+00:00

I am using the concept of level order traversal to generate Pascal Triangle. Here

  • 0

I am using the concept of level order traversal to generate Pascal Triangle.

Here is the code snippet:

void printPascalTriangle(int n)
{
    int line=1,Q[50]={0},f=-1,r=-1,prev,t;

    Enqueue(Q,&f,&r,1);
    Enqueue(Q,&f,&r,0);

    prev=0;
    while(line!=n)
    {
            t = Dequeue(Q,&f,&r);

            if( !t )
            {
                    line++;
                    prev = 0;
                    Enqueue(Q,&f,&r,1);

                    if(!isEmpty(&r))
                            Enqueue(Q,&f,&r,0);

                    printf("\n");
            }
            else
            {
                    printf("%d ", t);
                    Enqueue(Q,&f,&r,prev+t);
                    prev = t;
            }
    }
}

The complete code is here:

The advantage with this approach is that its space complexity is O(N) where N is the number of lines.

Is there any better way of doing the same?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:40:16+00:00Added an answer on June 10, 2026 at 5:40 am

    Since your only concern is space-complexity, and you’re using int (as opposed to some sort of big-integer type of potentially unbounded size), you can actually achieve space complexity of O(1) by using these two facts:

    • C(n, 0) = 1 for 0 ≤ n
    • C(n, k+1) = C(n, k) × (n − k) / (k + 1) for 0 ≤ k < n

    These two facts mean that you can compute each element using only the previous element of the same row, so you never have to save more than one element at a time.

    (By C(n, k) I mean the element in row n, position k of Pascal’s triangle, assuming that rows and positions are numbered starting from 0.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using views concept in my asp.net form control.my first view consist of
I am using views concept in my asp.net form control.my first view consist of
I am using multi threading concept to run some process. this process uses the
Is there any disadvantage of using URL expiring concept to protect online videos?
I am building a proof of concept for using Apache Camel. I am trying
What do you think about the concept of using ORO instead of traditional Garbage
I am trying to wrap my head around the concept of using macros to
I understand the concept and reasons behind using the using statement, and I use
I'm doing a proof of concept app in SL4 using MEF and as part
I understand the IOC concept, which we can mix-and-match different classes using wiring. Every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.