red_coder's blog

By red_coder, 12 years ago, In English

here is a question from codechef...

and here is my code ....

int main()
{
    int t;
    si(t);
    while(t--)
    {
        string A,B;
        cin.ignore();
        getline(cin,A);
        cin>>B;
        for(int i=0;i<A.size();i++)
        {
          if(A[i]==' ')
          continue;
          A[i]= B[(A[i]-'A')];
        }
        cout<<A<<"\n";
    }
    return 0;
}

why am i getting wrong answer afterall i am getting everything correct on my local compiler....

Full text and comments »

  • Vote: I like it
  • -23
  • Vote: I do not like it

By red_coder, 12 years ago, In English

suppose we are given N numbers a1,a2,a3....aN. Now how to write a program which calculates the Lcm of these numbers.....

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By red_coder, 12 years ago, In English

suppose we want to find a number that starts with N and is divisible by 2520. I found this statement written 'note that there is such number somewhere between N0000 and N2519, inclusive'. Now my question is HOW???????

Full text and comments »

  • Vote: I like it
  • -6
  • Vote: I do not like it

By red_coder, 12 years ago, In English

Can anyone give me some suggestions on how to improving my dynamic programming skills... any link, any pdf, anything.... i am having a lot of trouble ..... Please help me..

Full text and comments »

  • Vote: I like it
  • -8
  • Vote: I do not like it

By red_coder, 12 years ago, In English

suppose we are given a number 'N' and we want to count all integers >=1 and <=N that are divisible by 'A' or 'B' or 'C' and 'D' or 'E' or 'F' then the code for this would be:

int ans=0;
for(int i=1;i<=N;i++)
  if((i%A==0 || i%B==0 || i%C==0) && (i%D==0 || i%E==0 || i%F==0))
      ans++;

cout<<ans;

the above code would run in O(N) time; but suppose 'N' is too big then is there any faster way to accomplish the above task?????

Full text and comments »

  • Vote: I like it
  • -31
  • Vote: I do not like it

By red_coder, 12 years ago, In English

suppose for any 'n' taken as input we want to output (2n)!/n!(n+1)! modulo 1000000007 then which is the fastest way to accomplish this task......

Full text and comments »

  • Vote: I like it
  • -5
  • Vote: I do not like it

By red_coder, 12 years ago, In English

below is the function for adding some number to the end of the linked list.......

int add_at_end(struct node **q,int num)
{
  struct node *temp;
  if(*q==NULL)
  {
    temp= malloc(sizeof(struct node));
    temp->data=num;
    temp->link=NULL;
    *q=temp;
  }
  else
    {
      temp=*q;
      while(temp->link!=NULL)
	{
	  temp=temp->link;
	}
      r=malloc(sizeof(struct node));
      r->data=num;
      r->link=NULL;
      temp->link=r;
    }
  return 0;
}

now my question is what is the need of using double pointer for 'q' , why cant we do the same task using single pointer as below....

int add_at_end(struct node *q,int num)
{
  struct node *temp;
  if(q==NULL)
  {
    temp= malloc(sizeof(struct node));
    temp->data=num;
    temp->link=NULL;
    q=temp;
  }
  else
    {
      temp=q;
      while(temp->link!=NULL)
	{
	  temp=temp->link;
	}
      r=malloc(sizeof(struct node));
      r->data=num;
      r->link=NULL;
      temp->link=r;
    }
  return 0;
}

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it

By red_coder, 12 years ago, In English

suppose a class is as follows.. class cool { public: int a; int b; }; cool A[100];

now each element of array A[] has two sub elements 'a' and 'b'. Suppose we want to sort the array A[] in increasing order of 'a' and if two elements have equal 'a' then element with greater 'b' should be before. Can anyone give me the c++ code for the above purpose....

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it