Skip to content

Instantly share code, notes, and snippets.

@prrs
Created February 11, 2012 18:45
Show Gist options
  • Save prrs/1803503 to your computer and use it in GitHub Desktop.
Save prrs/1803503 to your computer and use it in GitHub Desktop.
interviewstreet
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void inline polyadd(unsigned short int A[],unsigned short int B[],unsigned short int C[], int);
void inline display(unsigned short int A[],int);
void inline polyset(unsigned short int A[],int,int);
int main()
{
unsigned short int A[6250]={0};
unsigned short int B[6250]={0};
unsigned short int C[6251]={0};
unsigned short int mask=0,mask1=0;
int n=0,q=0;char c;int i=0,size=0,coun_i=0;
scanf("%d%d",&n,&q);
//Reading the initial value of A & B
getchar();int a_c=0,a_r=0;
while((c=getchar())!='\n')
{
if(c=='1'){
mask1=1;
mask1=mask1<<a_c;
mask=mask|mask1;
mask1=0;
}
if(isdigit(c))
a_c++;
if(a_c==16){
A[a_r++]=mask;
mask=0;
a_c=0;
}
}
if(a_c!=0){
A[a_r]=mask;
}
i=0;a_r=0,a_c=0,mask=0,mask1=0;
while((c=getchar())!='\n')
{
if(c=='1'){
mask1=1;
mask1=mask1<<a_c;
mask=mask|mask1;
mask1=0;
}
if(isdigit(c))
a_c++;
if(a_c==16){
B[a_r++]=mask;
mask=0;
a_c=0;
}
}
if(a_c!=0){
B[a_r]=mask;
}
char input[10];
int index=0;
int set=0,r_c=0;
coun_i=0;
while(coun_i<q)
{
scanf("%s",input);
if(!strcmp("set_a",input))
{ scanf("%d%d",&index,&set);
polyset(A,index,set);
r_c=0;
}
else if(!strcmp("set_b",input))
{ scanf("%d%d",&index,&set);
polyset(B,index,set);
r_c=0;
}
else if(!strcmp("get_c",input))
{ scanf("%d",&index);
if(r_c==0){
C={0};
polyadd(A,B,C,n);
}
display(C,index);
r_c=1;
}
else
{}
coun_i++;
}
return 0;
}
void polyset(unsigned short int A[6250],int index,int set)
{
unsigned short a=0,b=0,c=0,d=0,f=0,set1=1,set2=65535;
a=index/16; b=index%16; c=set;d=c<<b;
f=A[a]&(set1<<b);
if(!set)
set2=set2^(set1<<b);
if(f)
A[a]=A[a]&set2;
else
A[a]=A[a]|d;
}
void polyadd(unsigned short int A[6250],unsigned short int B[6250],unsigned short int C[6251],int s)
{ unsigned int carry=0,i=0;
int a=s/16+1;
if(a>6250) a--;
for(i=0;i<a;i++)
{
C[i]=(A[i]+B[i]+carry)%65536;
carry=(A[i]+B[i]+carry)/65536;
}
if(carry && i==6250)
C[i]=carry;
}
void display(unsigned short int A[6251],int index)
{ int a=0,b=0,c=0,d=0,f=0,set=1,k=0;
a=index/16; b=index%16; c=set;d=c<<b;
k=A[a];
f=k&d;
if(f)
printf("%d",1);
else
printf("%d",0);
}
@prrs
Copy link
Author

prrs commented Feb 11, 2012

This is the solution for the puzzle "Changing Bits" from the interviewstreet. The code is not optimized. Please suggest what should be the approach for the same.

@prrs
Copy link
Author

prrs commented Feb 20, 2012

The above code is failing for some 4 hidden test cases....could some body has any idea what could be it.

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