- Programming Problem - Flipping coins
- 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>0)
- input = new int[N/32 + 1];
- else
- input = new int[N/32];
- //initialize to 0 = tails
- for(int i=0;i<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>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 => if coins are to be flipped
- if(command==0) {
- //if the coins range is over multiple numbers in array input[]
- if(RangeR/32>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 > 1) {
- for(int i=RangeL/32+1; i <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 && RangeR%32==0)
- input[RangeL/32] = input[RangeL/32] ^
- Integer.MIN_VALUE;
- else if(RangeL%32==0 && RangeR%32==1)
- input[RangeL/32] = input[RangeL/32] ^
- (Integer.MIN_VALUE + (int) Math.pow(2, 30));
- else if(RangeL%32==0 && RangeR%32 >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 && RangeR%32 ==1)
- input[RangeL/32] = input[RangeL/32] ^
- (int) Math.pow(2, 30);
- else if(RangeL%32==1 && RangeR%32 >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 => 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]<< RangeL%32;
- temp1 = temp1 >>> RangeL%32;
- temp1 = temp1 >>> (31 - RangeR%32);
- temp1 = temp1 << (31 - RangeR%32);
- output[out] = Integer.bitCount(temp1);
- }
- //if the coins range is over multiple numbers in array input[]
- else if(RangeR/32>RangeL/32) {
- temp1 = input[RangeL/32]<< RangeL%32;
- temp1 = temp1 >>> RangeL%32;
- temp2 = input[RangeL/32] >>> (31 - RangeR%32);
- temp2 = temp2 << (31 - RangeR%32);
- output[out] =
- Integer.bitCount(temp1)+ Integer.bitCount(temp2);
- }
- if(RangeR/32 - RangeL/32 > 1) {
- for(int i=RangeL/32+1; i <RangeR/32 ; i++) {
- output[out] = output[out] + Integer.bitCount(input[i]);
- }
- }
- out++;
- }
- Q--;
- }
- for(int i =0;i<out;i++) {
- System.out.println(output[i]);
- }
- }
- }
- 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>0)
- input = new int[N/32 + 1];
- else
- input = new int[N/32];
- //initialize to 0 = tails
- for(int i=0;i<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>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 => if coins are to be flipped
- if(command==0) {
- //if the coins range is over multiple numbers in array input[]
- if(RangeR/32>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 > 1) {
- for(int i=RangeL/32+1; i <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 && RangeR%32==0)
- input[RangeL/32] = input[RangeL/32] ^
- Integer.MIN_VALUE;
- else if(RangeL%32==0 && RangeR%32==1)
- input[RangeL/32] = input[RangeL/32] ^
- (Integer.MIN_VALUE + (int) Math.pow(2, 30));
- else if(RangeL%32==0 && RangeR%32 >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 && RangeR%32 ==1)
- input[RangeL/32] = input[RangeL/32] ^
- (int) Math.pow(2, 30);
- else if(RangeL%32==1 && RangeR%32 >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 => 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]<< RangeL%32;
- temp1 = temp1 >>> RangeL%32;
- temp1 = temp1 >>> (31 - RangeR%32);
- temp1 = temp1 << (31 - RangeR%32);
- output[out] = Integer.bitCount(temp1);
- }
- //if the coins range is over multiple numbers in array input[]
- else if(RangeR/32>RangeL/32) {
- temp1 = input[RangeL/32]<< RangeL%32;
- temp1 = temp1 >>> RangeL%32;
- temp2 = input[RangeL/32] >>> (31 - RangeR%32);
- temp2 = temp2 << (31 - RangeR%32);
- output[out] =
- Integer.bitCount(temp1)+ Integer.bitCount(temp2);
- }
- if(RangeR/32 - RangeL/32 > 1) {
- for(int i=RangeL/32+1; i <RangeR/32 ; i++) {
- output[out] = output[out] + Integer.bitCount(input[i]);
- }
- }
- out++;
- }
- Q--;
- }
- for(int i =0;i<out;i++) {
- System.out.println(output[i]);
- }
- }
- }
- int[] output = new int[Q];
- int[] input;
- //input - array of integers (one integer for 32 coins)
- if(N%32>0)
- input = new int[N/32 + 1];
- else
- input = new int[N/32];
- s = in.nextLine();
- str = s.split(" ");