I've been trying to solve this question for quite a while, given to us by our discrete maths professor. I've been having a hard time in general with it, so I thought I tried looking it up online but couldn't find anything much useful. Any help will be appreicatted in solving this.

A binary string consists of ones and zeros. Ram tells you number of occurences of 00, 01, 10, and 11 respectively in the string. Come up with an algorithm assuming those values to be variables to find the number of such possible strings. Note that if the number of occurences of 11,10,00, and 01 are 1, 1, 2, 1 respectively, you can come up with 5 such binary strings that satisfy it.

I did come up with a recursive formulation for this, that is f(p, q, r, s) where those are the variables respectively. From a theoretical CS perspective, it would take $O(2*p*q*r*s)$ time to compute. Our professor has specifically told it can be done better, just I dont know how better.

  • $\begingroup$ Before "looking it up online", did you check that the last sentence is true? $\endgroup$ – leonbloy Jan 29 '16 at 18:57
  • $\begingroup$ @leonbloy sorry I mis-typed the order. It's 11,10,00, and 01 => (1, 1, 2, 1). The strings will be 110001,100011,011000,001100,000110 $\endgroup$ – Play Boy Jan 29 '16 at 18:59
  • $\begingroup$ @PlayBoy: Hi, and welcome to MSE! $\endgroup$ – Eli Rose Jan 29 '16 at 19:00
  • 2
    $\begingroup$ Ongoing competition: hackerrank.com/contests/worldcodesprint/challenges/… Such a coincidence that your professor gave this problem just during the competition :-). Has he gave any other questions from the competition? $\endgroup$ – Salvador Dali Jan 30 '16 at 12:11
  • 1
    $\begingroup$ @DanielFischer unfortunately you have to register there to be able to see a list of problems. The problem there was absolutely the same as posted here (even the example with 1, 1, 2, 1). Right now the competition finished, and everyone can see the solutions, so this guy is surely no longer interested in the solution :-). $\endgroup$ – Salvador Dali Jan 30 '16 at 21:30

If we draw a directed multigraph with two nodes, 0 and 1, and draw $p$ edges from 1 to 1, $q$ edges from 1 to 0, etc.

          ___   ------>  ___
     --\ /   \ /        /   \/--
  p |   |  1  |        |  0  |  | r
     --> \___/ <------/ \___/<--

then the number of such strings is the number of different paths that visit every edge in the graph. (Actually, it's twice that number, since you can start at $0$ or at $1$).

I'm not sure if this can be converted into a more efficient algorithm than already given, however.

  • $\begingroup$ If I'm not wrong this is essentially the same thing as the recursion. Do you know of anything more efficient? $\endgroup$ – Play Boy Jan 29 '16 at 19:24
  • $\begingroup$ You are not wrong, and I do not know of anything more efficient. $\endgroup$ – Eli Rose Jan 29 '16 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.