title: Solution of twoTwo riddle on codingBat in Java
body: <p>The question is about Solving <a href="http://codingbat.com/prob/p102145" rel="nofollow">this</a> problem from codingBat in Java.</p>
  
  <p><strong>Problem Statement:</strong></p>
  
  <p><em>Given an array of ints, return true if every 2 that appears in the array is next to another 2.</em></p>
  
  <p><em>twoTwo({4, 2, 2, 3}) → true</em></p>
  
  <p><em>twoTwo({2, 2, 4}) → true</em></p>
  
  <p><em>twoTwo({2, 2, 4, 2}) → false</em></p>
  
  <p>First of all going by the problem statement that <em>every 2 that appears in the array is next to another 2.</em>  then </p>
  
  <blockquote>
    <p>do you think as suggested the outcome for the first inputs shown above
    should be true?</p>
  </blockquote>
  
  <p><strong><em>twoTwo({4, 2, 2, 3}) → true</em></strong></p>
  
  <p>Because as I see it it the <strong>first 2 itself that appears in the array is next to 4 not 2</strong></p>
  
  <p><strong>am I confused or it's a wrongly stated question?</strong> I had to grapple with the problem to somehow get the right code to crack the problem as below but it seems a hotch potch:</p>
  
  <pre><code>public boolean twoTwo(int[] nums) {
    if(nums.length==0)
    {
      return true;
     }
     if(nums.length==1)
    {
      return !(nums[0]==2);
     }
     if((nums.length==2))
     {
       if((nums[1]==2)&amp;&amp;(nums[0]==2))
         return true;
         else
       return false;
      }
     for(int i=0;i+2&lt;nums.length;i++)
     {
       if((nums[i]!=2)&amp;&amp;(nums[i+1]==2)&amp;&amp;(nums[i+2]!=2))
        return false;
     }
    if((nums[nums.length-2]!=2)&amp;&amp;(nums[nums.length-1]==2))
      return false;
  
  return true;     
  }
  </code></pre>
  
  <p>Any efficient alternate solutions are welcome.</p>
  
  <p>Thanks!</p>