here is only basic implementation of problems for beginners. If you have any problem with any solution or any basic concept of programming or you want more efficient solution you can mail me.
my suggestion is not to copy and paste codes from here try to understand the logic and think why you were not able to solve it.

Saturday 24 January 2015

Faridi and Yadav

problem statement is here


#include<stdio.h>
#include<math.h>

int main(){
double x,y,r,t;
scanf("%lf",&t);
while(t--){
scanf("%lf%lf",&x,&y);
r=2*(sqrt((x*x)-(y*y)));
printf("%.3lf\n",r );
}
return 0;
}

Party

problem statement is here


#include <algorithm>
#include <cstdio>
using namespace std;

int main() {
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
printf("%d\n", max(0, n-2));
}
return 0;
}

Count on Cantor

problem statement is here


#include<stdio.h>

int main()
{
    long long int t,a,b,c,i,j,k;
    scanf("%lld",&t);
    while(t--){
               scanf("%lld",&a);
               for(i=0; ;i++){
                        if(((i*(i+1))/2)<a && a<=(((i+1)*(i+2))/2) ){
                                           b=a-((i*(i+1))/2);
                                           break;
                                           }
                        }
                        c=i+1;
                        if(c%2==0){
                                   j=b;
                                   k=c+1-b;
                                   }else{
                                         j=c+1-b;
                                         k=b;
                                         }
                        printf("TERM %lld IS %lld/%lld\n",a,j,k);
               }
    return 0;
}

Black Widow Rings

problem statement is here


#include<stdio.h>
int main(){
long int max,t,n,ar[10000],br[10000],i,j,p,m;
scanf("%ld",&t);
while(t--){
max=0;m=0;
scanf("%ld",&n);
for(i=0;i<n;i++){
scanf("%ld %ld",&ar[i],&br[i]);
if(ar[i]>max){
max=ar[i];
p=i;
}
}
br[p]=0;
for(i=0;i<n;i++){
if(m<br[i]){
m=br[i];
}
}
if(max>m){
printf("%ld\n",p+1);
}else{
printf("-1\n");
}
}
return 0;
}

Monday 12 January 2015

Pattern Find

problem statement is here


it is a basic concept of KMP algorithm
if u want to read about KMP algorithm u can find it here

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int zr[1000002],q;
void computeLPSArray(char *pat, int M, int *lps){
    int len=0,i=1;
    lps[0]=0;
    while (i < M){
       if (pat[i] == pat[len]){
         len++;
         lps[i] = len;
         i++;
       }else{
         if (len != 0){
           len = lps[len-1];
         }else{
           lps[i] = 0;
           i++;
         }
       }
    }
}
void KMPSearch(char *pat, char *txt){
    int M = strlen(pat);
    int N = strlen(txt);
    int *lps = (int *)malloc(sizeof(int)*M);
    int j=0,i=0; 
    computeLPSArray(pat, M, lps);
    while (i < N){
      if(pat[j] == txt[i]){
        j++;
        i++;
      }
   if (j == M){
        zr[q++]=i-j+1;
        j = lps[j-1];
      }else if (i < N && pat[j] != txt[i]){
        if (j != 0)
         j = lps[j-1];
        else
         i = i+1;
      }
    }
    free(lps);
}
int main(){
   int t,i;
   scanf("%d",&t);
   while(t--){
    q=0;
    char ar[1000006],br[1000005];
    scanf("%s %s",ar,br);
    KMPSearch(br,ar);
    if(q==0){
    printf("Not Found\n");
    }else{
    printf("%d\n",q);
    for(i=0;i<q;i++){
    printf("%d ",zr[i]);
    }
    printf("\n");
    }

   }
   return 0;
}