title: Detect nth term of binary sequence
body: <p>I have a binary sequence that follows a specific logic such that
  it starts with <code>0</code> and </p>
  
  <p>nth term = sequence of (n-1)th term + <code>0</code> + inverse(reverse(n-1)th term)</p>
  
  <p>eg:</p>
  
  <pre><code>0
  0 + 0 + 1
  001 + 0 + 011
  0010011 + 0 + 0011011
  </code></pre>
  
  <p>Here, I need to find out the nth term of the kth sequence.</p>
  
  <p>My take:</p>
  
  <p>I've written a recursive function to calculate the number of terms in k'th sequence</p>
  
  <pre><code>  public static long noOfElements(long elements){
       long answer;
       if(elements == 1)
          return 1;
       else
       answer = 1 + 2*noOfElements(elements-1);
       return answer;
  }
  </code></pre>
  
  <p>After analysis, I found out the sequence follows a certain pattern, the k'th sequence can be broken down by half and switching the values of 0 and 1 I could keep track of the result.</p>
  
  <p>So, My function below breaks the given sequence down to [0,0,1] recursively</p>
  
  <pre><code>public static long letsBegin(long reqd, long length){
      long mid = (length + 1)/2;
      if(length &lt;= 3){
          return reqd;
      }else{
          if(reqd &gt; mid){
              reqd = reqd - 2*(reqd-mid);
              switcher();                 //Switcher stores if the value is switched
              return letsBegin(reqd, mid);
          }else{
              return letsBegin(reqd, mid);
          }
      }
  }
  </code></pre>
  
  <p>In the end I have index 1, 2 or 3 in [0,0,1] and I output the value accordingly.
  The problem here is</p>
  
  <ol>
  <li>It fails for some unknown case (Probably my logic's wrong).</li>
  <li>The number of sequences can be upto 50, making number of elements = 1125899906842623 and hence, takes too long to output value (>2sec)
  What may have went wrong? Is my logic incorrect</li>
  </ol>