title: Programming Problem - Flipping coins
body: <p>I need to optimize the following code to take less memory.
  I cant find out where the memory is being wasted. It is giving the correct results.
  The problem statement can be seen here <a href="http://www.codechef.com/problems/FLIPCOIN/" rel="nofollow">http://www.codechef.com/problems/FLIPCOIN/</a></p>
  
  <pre><code>import java.util.Scanner;
  
  public class FLIPCOIN4 {
      public static void main(String args[])
      {
          Scanner in = new Scanner(System.in);
          //take N = no of coins, Q = no of commands as input
          String s =  in.nextLine();
          String[] str = s.split(" ");
          int N = Integer.parseInt(str[0]);
          int Q= Integer.parseInt(str[1]);
          int[] output = new int[Q];
          int[] input;
          //input - array of integers (one integer for 32 coins)
          if(N%32&gt;0)
              input = new int[N/32 + 1];
          else
              input = new int[N/32];
          //initialize to 0 = tails
          for(int i=0;i&lt;input.length;i++)
              {
                  input[i] = 0;
  
              }
          int out = 0;
          int temp1 = 0;
          int temp2 = 0;
          int command = 0;
          int RangeL = 0;
          int RangeR = 0;
          //looping over all Q commands
          while(Q&gt;0) {
  
              s = in.nextLine();
              str = s.split(" ");
              //command - command code
              //RangeL,RangeR - range of coins which are affected
              command = Integer.parseInt(str[0]);
              RangeL = Integer.parseInt(str[1]);
              RangeR = Integer.parseInt(str[2]);
              // command==0 =&gt; if coins are to be flipped
              if(command==0) {
                  //if the coins range is over multiple numbers in array input[]
                  if(RangeR/32&gt;RangeL/32) {
                      if(RangeL%32==0)
                          input[RangeL/32] = input[RangeL/32] ^ -1;
                      else if(RangeL%32==1)
                          input[RangeL/32] =
                              input[RangeL/32] ^ Integer.MAX_VALUE;
                      else
                          input[RangeL/32] =
                              input[RangeL/32] ^ (int)Math.pow(2, 32-(RangeL%32));
  
                      if(RangeR%32==0)
                          input[RangeR/32] = input[RangeL/8] ^ Integer.MIN_VALUE;
                      else 
                          input[RangeR/32] =
                              input[RangeR/8] ^
                              (Integer.MIN_VALUE
                               + ( Integer.MAX_VALUE +1-
                                   (int) Math.pow(2, 31-(RangeL%32))));
                      if(RangeR/32 - RangeL/32 &gt; 1) {
                          for(int i=RangeL/32+1; i &lt;RangeR/32 ; i++) {
                              input[i] = input[i] ^ -1;
                          }
                      }
                  }//if the coins range is contained in one single integer in input[]
                  else if(RangeR/32==RangeL/32) {
                      if(RangeL%32==0 &amp;&amp; RangeR%32==0)
                          input[RangeL/32] = input[RangeL/32] ^
                              Integer.MIN_VALUE;
                      else if(RangeL%32==0 &amp;&amp; RangeR%32==1)
                          input[RangeL/32] = input[RangeL/32] ^ 
                              (Integer.MIN_VALUE + (int) Math.pow(2, 30));
                      else if(RangeL%32==0 &amp;&amp; RangeR%32 &gt;1)
                          input[RangeL/32] = input[RangeL/32] ^ 
                              (Integer.MIN_VALUE +  Integer.MAX_VALUE +1 -
                               (int) Math.pow(2, 31-(RangeR%32)));
                      else if(RangeL%32==1 &amp;&amp; RangeR%32 ==1)
                          input[RangeL/32] = input[RangeL/32] ^ 
                              (int) Math.pow(2, 30);
                      else if(RangeL%32==1 &amp;&amp; RangeR%32 &gt;1)
                          input[RangeL/32] = input[RangeL/32] ^ 
                              (Integer.MAX_VALUE +1 -
                               (int) Math.pow(2, 31-(RangeR%32)));
                      else
                          input[RangeL/32] = input[RangeL/32] ^
                              ((int) Math.pow(2, 32-(RangeL%32)) -
                               (int) Math.pow(2, 31-(RangeR%32)) );
  
  
                  }
              }// command==1 =&gt; no of heads is to be reported
              else if(command==1) {//if the coins range is contained in a single integer
                  if(RangeR/32 == RangeL/32) {
                      temp1 = input[RangeL/32]&lt;&lt; RangeL%32;
                      temp1 = temp1 &gt;&gt;&gt; RangeL%32;
                      temp1 = temp1 &gt;&gt;&gt; (31 - RangeR%32);
                      temp1 = temp1 &lt;&lt; (31 - RangeR%32);
                      output[out] = Integer.bitCount(temp1);
                  }
                  //if the coins range is over multiple numbers in array input[]
                  else if(RangeR/32&gt;RangeL/32) {
                      temp1 = input[RangeL/32]&lt;&lt; RangeL%32;
                      temp1 = temp1 &gt;&gt;&gt; RangeL%32;
  
                      temp2 = input[RangeL/32] &gt;&gt;&gt; (31 - RangeR%32);
  
                      temp2 = temp2 &lt;&lt; (31 - RangeR%32);
                      output[out] =
                          Integer.bitCount(temp1)+ Integer.bitCount(temp2);
  
                  }
  
                  if(RangeR/32 - RangeL/32 &gt; 1) {
                      for(int i=RangeL/32+1; i &lt;RangeR/32 ; i++) {
                          output[out] = output[out] + Integer.bitCount(input[i]);
                      }
                  }
                  out++;
              }
              Q--;
          }
          for(int i =0;i&lt;out;i++) {
              System.out.println(output[i]);
          }
      }
  }
  </code></pre>