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 7877585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:24:53+00:00 2026-06-03T03:24:53+00:00

You have some LEGO plastic bricks, all bricks are 1x1x1. Also you have one

  • 0

You have some LEGO plastic bricks, all bricks are 1x1x1. Also you have one tile, 1xN (N <= 80), on which you should place the LEGO bricks. You can order them in sequences (one sequence is correct if it has 3 or more consecutive bricks), also you must have at least 1 empty space between 2 sequences. You should calculate the number of different combination you can place the bricks on the tiles.

Here’s an example:

If the tile is 1×7, there are 17 different combinations.

input : 7
output : 17

pic of the example
(source: mendo.mk)

Also if you have no bricks it’s counted as 1 combination.

I have worked on this problem and i found the way to calculate possible combinations of if the max length of the tile is 14 (3 sequences). I found it using for loops.

My biggest problem is the huge number of for loops that i need to run. For example, for 1 sequence i use 1 for loops, for 2 sequences 2 loops + 1 for 1 sequnce…so if i use all 80 bricks, i can create 20 sequence and i will have to use over 210 for loops which is huge number. So it’ll be good if i can nest them in one. I tried it and it get messy and it doesn’t give correct answers.

New code:

#include <iostream>
using namespace std;
int main()
{
    long long int places, combinations = 1;
    cin >> places;
    long long int f[80], g[80];
    f[0] = 0;
    f[1] = 0;
    f[2] = 0;
    g[0] = 1;
    g[1] = 1;
    g[2] = 1;
    for(int i = 3; i<=places; i++)
    {
        f[i] = f[i-1] + g[i-3];
        g[i] = f[i-1] + g[i-1];
    }
    combinations = f[places] + g[places];
    cout << combinations;
    return 0;
}
  • 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-03T03:24:54+00:00Added an answer on June 3, 2026 at 3:24 am

    If this is a counting problem (not outputting combination, rather just counting them) it’s an easy one. Assume we solved it for n ≥ 3 now to solve it for n+1, we solve it by induction:

    Assume f is a function that shows the number of possible ways such that the last item is a brick.
    Analogously g is a function that shows the number of possible ways such that the last item is not brick.
    Let define h = f+g, to be the number of all possible ways.

    So we have:

    f(n+1) = f(n) + g(n-2)
    g(n+1) = g(n) + f(n)
    

    With initial condition:

    for n=0,1,2: g=1, f= 0.
    for n = 3: g=1,f=1
    

    Samples:

    n=4: g=2,f=2 ==> h=4
    n=5: g=4, f= 3 ==> h=7
    n=6: g=7, f= 4 ==> h=11
    n=7: g=11,f=6 ==> h=17
    

    We can solve it with one for loop in O(n).


    Why:

    f(n+1) = f(n) + g(n-2)
    g(n+1) = g(n) + f(n)
    

    First, let prove first part:

    Remember that we assumed f(n) is a working solution which has a plastic brick in the last item, and g(n) is a working solution which doesn’t have a brick in the last item.

    f(n+1) can be obtained from f(n) by adding one brick at the last place.
    Also f(n+1) can be obtained by adding three brick after g(n-2), it means cells of n-1,n,n+1.

    Note that we can’t add brick after g(n-1) or g(n) to create a valid solution for f(n+1) because they are not valid solutions (number of consecutive bricks is less than 3). Also, note that we don’t need to count the number of ways which arises by adding bricks after g(n-3) because they are previously enumerated by f(n). So we have f(n+1) = f(n) + g(n-2).

    In the same way we can prove g(n+1) = f(n)+g(n) this case is easier, because g(n+1) simply can be made from any valid solution up to n, as there is no 3 consecutive brick barrier here, they can come after any valid solution.

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

Sidebar

Related Questions

I have an arbitrary array of lego bricks. I also have some figures made
We have some solution that we built against a MOSS farm one of which
I have some 'legacy' code (which I can't change, but need to add on
I have some trouble with date manipulation in Javascript. I have one variable which
Have some text that needs to be replaced, searched around this website for all
I have some classes all having a constructor that takes a file path. I
I have some text which I want to display in some sort of log
Have some method, which is called sync like: tasks.ForEach(p=> Proccess(p)); void Proccess(){ //do some
I have some JavaScript which is changing an image correctly but once it has
I have some source code which I had developed and later gave to another

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.