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.
Showing posts with label hackerrank. Show all posts
Showing posts with label hackerrank. Show all posts

Saturday, 27 September 2014

Hackerrank candies

// https://www.hackerrank.com/challenges/candies

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

int main() {

    long a=0,n,i,j,ar[100003],dp[100003];
    scanf("%ld",&n);
    scanf("%ld",&ar[0]);
    dp[0]=1;
    for(i=1;i<n;i++){
        scanf("%ld",&ar[i]);
        if(ar[i]>ar[i-1]){
            dp[i]=dp[i-1]+1;
        }else{
            dp[i]=1;
        }
    }
    for(i=n-2;i>=0;i--){
        if(ar[i]>ar[i+1] && dp[i]<=dp[i+1]){
            dp[i]=dp[i+1]+1;
        }
    }
    for(i=0;i<n;i++){
        //printf("%ld  ",dp[i]);
        a+=dp[i];
    }
    printf("%ld\n",a);
    return 0;
}

red john is back

// https://www.hackerrank.com/challenges/red-john-is-back

problem statement is here


#include<stdio.h>
int ar[300000]={0};
long long fact(long long a){
long long i,b=1;
for(i=2;i<=a;i++){
b*=i;
}
return b;
}
int main(){
    long long x,a,b,c,d,e,i,j,z;
    ar[0]=ar[1]=1;
    for(i=2;i<3000;i++){
    if(ar[i]==0){
    for(j=i*2;j<300000;j+=i){
    ar[j]=1;
    }
    }
    }
scanf("%lld",&d);
    while(d--){
        scanf("%lld",&a);
        if(a<4){
            b=1;
        }else{
            e=a/4;
            b=1;
            for(i=1;i<=e;i++){
            z=1;
            for(x=a-i*4+1;x<=a-i*4+i;x++){
            z*=x;
            }
            z/=fact(i);
            // printf("z=%lld\n",z);
            b+=z;
            }
        }
        c=0;
        for(i=2;i<=b;i++){
            if(ar[i]==0){
                c++;
            }
        }
        printf("%lld\n",c);
    }
 
    return 0;
}

Thursday, 25 September 2014

Spoj alia and 3 khans

// http://www.spoj.com/problems/KHANS/

#include<stdio.h>
int main(){
long long t,a,b,n,c,i,j,k,x,u,p=0,s=0,q,l;
int ar[100],br[100];
scanf("%lld",&t);
q=t;
while(t--){
scanf("%lld",&n);
a=n;i=0,j=0,k=0;
while(a>0){
ar[i]=a%2;
a/=2;
br[i]=ar[i];
i++;
}
// printf("%lld\n",i);
x=0;
for(j=0;j<i;j++){
if(ar[j]==0){
if(ar[j+1]==1){
u=ar[j];
ar[j]=ar[j+1];
ar[j+1]=u;
break;
}
}
if(ar[j]==1){
x++;
}
}
//printf("%lld\n",j);
if(j==i){
b=-1;
}else{
for(k=j-1;k>=0;k--){
if(x>0){
ar[k]=1;
x--;
}else{
ar[k]=0;
}
}
b=0;l=1;
for(j=0;j<i;j++){

b+=(ar[j]*l);
l*=2;
// printf("%d  ",mult(2,j));
}
}
ar[i]=0;
br[i]=0;
i++;
x=0;j=0;k=0;
for(j=0;j<i;j++){

if(br[j]==1){
if(br[j+1]==0){
u=br[j];
br[j]=br[j+1];
br[j+1]=u;
break;
}
}
if(br[j]==1){
x++;
}
}
// printf("%lld\n",x);
for(k=0;k<=j-1;k++){
if(x>0){
br[k]=1;
x--;
}else{
br[k]=0;
}
// printf("%d  ",br[k]);
}
c=0;l=1;
for(j=0;j<i;j++){
c+=(br[j]*l);
l*=2;
}
if(n==0){
c=-1;
}
//printf(" %lld %lld %lld\n",n,b,c);
if(n*n==b*c){
p++;
}
s+=(c-b);
//printf("%lld %lld\n",p,s);
}
double y,z;
y=(double)p/(double)q;
z=(double)s/(double)q;
printf("%.6lf %.6lf\n",y,z);
return 0;
}

Wednesday, 24 September 2014

Spoj alia and handsome devil

// http://www.spoj.com/problems/HDEVIL/

#include<stdio.h>
#include<math.h>
int main(){
long long a,b,c=1,i,j,t,n,ar[1000],m;
ar[0]=0;ar[1]=1;ar[2]=1;
for(i=3;i<100;i++){
ar[i]=ar[i-1]+ar[i-2];
}
// printf("%lld\n",ar[94]);
scanf("%lld",&t);
while(t--){
scanf("%lld %lld",&n,&m);
long long sum=0,sum1=0;
b=0;
a=sqrt(n);
if(a*a==n){
sum+=a;
a--;

}
for(i=2;i<=a;i++){
if(n%i==0){
sum+=i;
sum+=(n/i);
//printf("%lld\n",sum);
}
}
sum+=1;
sum%=m;
a=sqrt(sum);
if(a*a==sum){
sum1+=1;
a--;
}
for(i=2;i<=a;i++){
if(sum%i==0){
sum1+=2;
}
}
sum1+=1;
// printf("%lld\n",sum1);
for(i=0;i<96;i++){
if(ar[i]==sum1){
b=1;
break;
}
}
if(b==1){
printf("Case #%lld : YES.\n",c);
}else{
printf("Case #%lld : NO.\n",c);
}
c++;
}

return 0;
}