title: To reach the target number from 0 in minimum number of steps
body: <p><strong>Problem Statement:</strong>
  Find the minimum number of steps required to reach a target number x from 0 (zero), using only two operations: +1 <em>(add 1 to the number)</em> or *2 <em>(multiply 2 with the number)</em>.</p>
  
  <p><strong>So here's the Logic that I came up with:</strong></p>
  
  <p>The best way is to work backwards. Start from the number you need:</p>
  
  <ol>
  <li>Subtract 1 if the number is odd. </li>
  <li>Divide by 2 if the number if even.</li>
  <li>Stop when you get to zero.</li>
  </ol>
  
  <p>For example, for 29, you get 28, 14, 7, 6, 3, 2, 1, 0.</p>
  
  <p><strong>And, here's what I have tried doing (Java 7):</strong></p>
  
  <p><code>kValues</code> is an array that has the <code>x</code> values for which the <code>steps</code> are needed to be computed and stored in an array called <code>result</code>.</p>
  
  <pre><code>static int[] countOperationsToK(long[] kValues) {
      int size = kValues.length,x,i,steps;
      int result[] = new int[size];
  
      for (i = 0; i &lt; size; ++i)
      {
          steps = 0;
          for (x = (int)kValues[i]; x != 0 ; ++steps)
          {
              if((x % 2) == 0)
                  x /= 2;
              else x--;
          }
          result[i] = steps;
      }
  
      return result;
  }
  </code></pre>
  
  <p><strong>My Problem:</strong></p>
  
  <p>This is a Hackerrank question and I am supposed to write an efficient code. I was successful with 7/11 test cases and others were timed out. Since, it is a Hackerrank question, I can't change the function definition or the return type. That is the reason why I am converting from <code>long</code> to <code>int</code> in my <code>for</code> loop, in order to use <code>%</code> (modulus). I would like to know where I am going wrong. Is my algorithm taking too long to compute (for the number of values close to a million)? Which is obviously the case, but how do I alter my algorithm in order to pass all the test cases?</p>
  
  <p>Thank you in advance :) </p>