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

  • Home
  • SEARCH
  • 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 9221519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:35:49+00:00 2026-06-18T03:35:49+00:00

Given a binary digit count of n, and a maximum consecutive occurrence count of

  • 0

Given a binary digit count of n, and a maximum consecutive occurrence count of m, find the number of different possible binary numbers. Also, the leftmost and rightmost bit must be 1.

For example n = 5, and m = 3.

The count is 7:
10001
10011
10101
10111
11001
11011
11101

Notice we excluded 11111 because too many consecutive 1’s exist in it.

This was an interview question I had recently, and It has been bothering me. I don’t want to brute force check each number for legitimacy because n can be > 32.

  • 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-18T03:35:51+00:00Added an answer on June 18, 2026 at 3:35 am

    Let’s call a binary sequence almost valid if it starts with “1” and has at most m consecutive “1” digits.

    For i = 1, ..., n and j = 0, ..., m let a(i, j) be the number of almost valid sequences with length i that end with exactly j consecutive “1” digits.

    Then

    • a(1, 1) = 1 and a(1, j) = 0 for j != 1, because “1” is the only almost valid sequence of length one.
    • For n >= 2 and j = 0 we have a(i, 0) = a(i-1, 0) + a(i-1, 1) + ... + a(i-1, m), because appending “0” to any almost valid sequence of length i-1 gives an almost valid sequence of length i ending with “0”.
    • For n >= 2 and j > 0 we have a(i, j) = a(i-1, j-1) because appending “1” to an almost valid sequence with i-1 trailing ones gives an almost valid sequence of length j with i trailing ones.

    Finally, the wanted number is the number of almost valid sequences with length n that have a trailing “1”, so this is

    f(n, m) = a(n, 1) + a(n, 2) + ... + a(n, m)
    

    Written as a C function:

    int a[NMAX+1][MMAX+1];
    int f(int n, int m)
    {
        int i, j, s;
    
        // compute a(1, j):
        for (j = 0; j <= m; j++)
            a[1][j] = (j == 1);
    
        for (i = 2; i <= n; i++) {
            // compute a(i, 0):
            s = 0;
            for (j = 0; j <= m; j++)
                s += a[i-1][j];
            a[i][0] = s;
    
            // compute a(i, j):
            for (j = 1; j <= m; j++)
                a[i][j] = a[i-1][j-1];
        }
    
        // final result:
        s = 0;
        for (j = 1; j <= m; j++)
            s += a[n][j];
        return s;
    }
    

    The storage requirement could even be improved, because only the last column of the matrix a is needed. The runtime complexity is O(n*m).

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

Sidebar

Related Questions

Given a binary search tree (BST). Find the maximum depth of the binary search
For a given binary tree, find the largest subtree which is also binary search
I saw this problem: Given a binary search tree and a number, find a
I'm given three binary random variables: X, Y, and Z. I'm also given the
Given a binary number, what is the fastest way of removing the lowest order
Given a binary search tree and an integer K, i would like to find
Given a tree (binary) structure, with depth N. Is it possible to compute some
How do I find the density of a given binary tree? I came across
Given an binary mask with an object in Matlab. I am going to find
Given a binary file compiled with OCaml, is there a way to find out

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.