Skip to content

Instantly share code, notes, and snippets.

@phaniram
Created February 15, 2012 17:59
Show Gist options
  • Save phaniram/1837802 to your computer and use it in GitHub Desktop.
Save phaniram/1837802 to your computer and use it in GitHub Desktop.
Interview Street Challenges - Changing Bits
package com.interviewstreet.puzzles;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
int bit;
int[] A;
int[] B;
String C_str;
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num_of_bits=sc.nextInt();
int num_of_queries=sc.nextInt();
Solution sh=new Solution(num_of_bits,sc.next(),sc.next());
while(--num_of_queries>=0)
{
String str=sc.next();
if(str.equals("set_a"))
{
sh.query_set_a(sc.nextInt(),sc.nextInt());
}else if(str.equals("set_b"))
{
sh.query_set_b(sc.nextInt(),sc.nextInt());
}else
{
sh.query_get_c(sc.nextInt());
}
}
}
private Solution(int num_of_bits, String A_str, String B_str) {
this.bit=num_of_bits;
A = new int[num_of_bits];
int i=0;
for (char c : A_str.toCharArray()) {
A[i++] = Character.digit(c,10);
}
B = new int[num_of_bits];
i=0;
for (char c : B_str.toCharArray()) {
B[i++] = Character.digit(c,10);
}
}
private void query_set_a(int idx, int x) {
A[bit-1-idx]=x;
}
private void query_set_b(int idx, int x) {
B[bit-1-idx]=x;
}
private void query_get_c(int idx) {
String a_bin_string=Arrays.toString(A).replace(" ","").replace(",","").replace("[","").replace("]","");
String b_bin_string=Arrays.toString(B).replace(" ","").replace(",","").replace("[","").replace("]","");
BigInteger a_bigInt = new BigInteger(a_bin_string, 2);
BigInteger b_bigInt = new BigInteger(b_bin_string, 2);
BigInteger c_bigInt= a_bigInt.add(b_bigInt);
C_str=c_bigInt.toString(2);
// System.out.println(C_str);
int len=C_str.length();
if(len==bit)
{
C_str="0".concat(C_str);
len+=1;
}
// System.out.print(C_str.charAt(len-1-idx));
// System.out.print(C_str+" - "+" idx- "+idx+" ");
System.out.print(C_str.charAt(len-1-idx));
}
}
@phaniram
Copy link
Author

Changing Bit (30 points)

Let A and B be two N bit numbers. You are given initial values for A and B, and you should write a program which processes three kinds of queries:

set_a idx x: Set A[idx] to x, where 0 <= idx < N, where A[idx] is idx'th least significant bit of A.
set_b idx x: Set B[idx] to x, where 0 <= idx < N.
get_c idx: Print C[idx], where C=A+B, and 0<=idx<N+1.
Input
First line of input contains two integers N and Q consecutively (1 <= N <= 100000, 1<= Q <= 500000). Second line is an N-bit binary number which denotes initial value of A, and the third line is an N-bit binary number denoting initial value of B. Q lines follow, each containing a query as described above.

Output
For each query of the type get_c, output a single digit 0 or 1. Output must be placed in a single line.

Sample Input
5 5
00000
11111
set_a 0 1
get_c 5
get_c 1
set_b 2 0
get_c 5

Sample Output
100

@prrs
Copy link

prrs commented Feb 17, 2012

Hi....how much test cases have u passed with this solution

@phaniram
Copy link
Author

i'm getting TLE after 1 testcase, hv to optimize my code

@prrs
Copy link

prrs commented Feb 17, 2012

git://gist.github.com/1803503.git

The above c++ code is passing for 4/11, for 4 test cases m getting wrong output and for 3 TLE. I can optimize it more but m not able to detect the testcases for which its failing.

@sparr
Copy link

sparr commented Mar 28, 2012

Here's a hint for your TLE phaniram: you don't have to add together all of A and B to find out a specific digit in C.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment