Showing posts with label adhoc. Show all posts
Showing posts with label adhoc. Show all posts

Friday, 11 September 2015

YELBRICK



The Yellow Brick Road

Link to the question : YELBRICK 

HINTS :

Very simple ad-hoc question. Just find the gcd of the length, breadth and height of the stones, and then find the number of stones that can be produced.

RECOMMENDED  QUESTION :

Try solving this question as even this requires some application of gcd.

SOURCE CODE :

#include <iostream>
using namespace std;
int gcd(int x,int y)
{
    while(x!=y){
          if(x>y)
              return gcd(x-y,y);
          else
             return gcd(x,y-x);
     }
     return x;
}
int main() 
{
    int n,i;
    while(1)
    {
        cin>>n;
        if(n==0)
        break;
        int a[n][3];
        long long vol=0;
        for(i=0;i<n;i++)
        {
            cin>>a[i][0]>>a[i][1]>>a[i][2];
        }
        int hcf =a[0][0];
        for(i=0;i<n;i++)
        {
            hcf = gcd(hcf,a[i][0]);
            hcf = gcd(hcf,a[i][1]);
            hcf = gcd(hcf,a[i][2]);
        }
        for(i=0;i<n;i++)
        {
            vol+=(long long)((a[i][0]/hcf)*(a[i][1]/hcf)*(a[i][2]/hcf));
        }
        cout<<vol<<"\n";
    }
    return 0;
}

Friday, 7 August 2015

KURUK14

GENIE SEQUENCE

Link to the question : KURUK14 

HINT :

It is said that a genie sequence is a sequence of numbers which can be arranged in such a way that they either tell the number of elements behind or number of elements in front of them. In order for a n elements sequence to follow the rule, if it contains c then it should also contain n-c or c, because if at the (c+1)th position we say there are c elements behind it, then at (n-c + 1)th we can say there are n-c elements behind it or c elements in front of it. So either 2 c's or 2 (n-c)'s  or 1 c and 1 (n-c) must be present. So in the case of 2 c's or 2 (n-c)'s we make them 1 c and 1 (n-c) then we will get all numbers from 0 to n-1 in the genie sequence. So we just need to check all 0 to n-1 numbers are present or not after converting repeating numbers to n-x form. 

SOURCE CODE :

/* Genie Sequence - (KURUK 14) */
/* Sushant Gupta */

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,f=0,c;
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
            a[i]= 0;
        for(i=0;i<n;i++)
        {
             scanf("%d",&c);
             if(c<n)
             {
                 if(a[c]==0)
                    a[c]=1;
                 else
                    a[n-1-c]=1;
             }

        }
        for(i=0;i<n;i++)
        {
            if(a[i]==0)
                f=1;
        }
        if(f==0)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
 

Friday, 24 July 2015

FASHION

Fashion Shows

Link to the question : FASHION 

HINT :

Simply just sort the array containing hotness level of men and women, then multiply the elements in the same index and keep on adding.

RECOMMENDED QUESTION :

Try solving this question   after getting an AC in this one.

SOURCE CODE :

  #include<iostream>
#include<new>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
     int *a,*b,n,s,i=0,t,j;
     cin>>t;
     while(t--)
     {
           cin>>s;
           i=0;
           a= new (nothrow)int[s];
           b= new (nothrow)int[s];
          for(j=0;j<s;j++)
                 cin>>a[j];
           sort(a,a+s);
             for(j=0;j<s;j++)
                  cin>>b[j];
              sort(b,b+s);
              for(j=0;j<s;j++)
              {
                  i= i+ a[j]*b[j];
              }
              cout<<i<<endl;
     }
 return 0;
}

BANDW


Black and White

Link to the question : BANDW 

HINT :

My solution is very basic.Let the input string be s[] and the objestive string be t[].  Run a loop till the end. If s[i] = t[i] continue, else if they are not equal, keep a count again when they are equal.
I think checking the source code once will make it more clear.

RECOMMENDED QUESTION :

Try this adhoc question after this one.

SOURCE CODE :

/* Black And White */
/* Sushant Gupta */

#include<stdio.h>
#include<string.h>
int main()
{
    char s[600],t[600];
    int i;
    s[0]= '0';

    while(s[0]!= '*')
    {
        scanf("%s%s",s,t);
        if(s[0]!= '*')
        {
            int n,c=0;
            n = strlen(s);
            i=0;
            while(i<n)
            {
                if(s[i]!=t[i]) {
                while(s[i]!=t[i])
                    i++;
                c++;}
                while(s[i]==t[i])
                    i++;
            }
            printf("%d\n",c);
        }
        else
            return 0;
    }
}
 

Wednesday, 22 July 2015

EGYPIZZA



Pizza

Link to the question : EGYPIZZA 

HINT :

Easy question. Just stick to the basics and code all the way.

RECOMMENDED QUESTION :

Try your hands on this question .

SOURCE CODE :



#include<iostream>

#include<cmath>

#include<cstdio>

using namespace std;

int main()

{

int n,i=0,ans=0;

int a1=0,a2=0,a3=0;

int n1,n2;

char op;

cin>>n;

for(i=0;i<n;i++)

{

cin >> n1 >> op >> n2;

if(n1==3) a3++;

else if(n2==2) a2++;

else if(n2==4) a1++;

}

ans=a3;

a1=a1-a3;

ans+=(a2-a2%2)/2;

if(a2%2)

{

ans++;

a1-=2;

}

if(a1 > 0) {

ans+=(a1-(a1%4))/4;

if(a1%4>0) {

ans++;

}

}

cout<<ans+1<<endl;

return 0;

}



    


Tuesday, 21 July 2015

COMDIV

Number of common divisors

Link to the question : COMDIV 

HINT : 

The number of common divisors of two numbers is simply the number of divisors of their gcd.

RECOMMENDED QUESTION :

Try solving this question .

SOURCE CODE :

/* SPOJ - Number Of Common Divisors (COMDIV)
         - Sushant Gupta   */

#include<stdio.h>

int hcf(int n1, int n2)
{
    if(n2>n1)
        return hcf(n2,n1);
    else if (n2!=0)
       return hcf(n2, n1%n2);

    else
       return n1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int a,b,c=0,i,h;
        scanf("%d%d",&a,&b);
        h= hcf(a,b);
        for(i=1;i*i<=h;i++)
        {
            if(h%i==0)
                c=c+2;

        }
        i=i-1;
        if(i*i==h)
            c--;
        printf("%d\n",c);

    }
    return 0;
}

CEQU


Crucial Equation

Link to the question : CEQU 

HINT : 

We need to find whether there exists an integer solution for x and y  which satisfy the equation ax + by = c. This can be done by finding the gcd of a and b and checking if it divides c.

RECOMMENDED QUESTION :

I think you will love solving a dp question after solving this one. So try your hands on this question .

SOURCE CODE :

#include<stdio.h>

gcd(int m,int n){

 if(n==0)

  return m;

 else 

  return gcd(n,m%n);

}

int main(){

 int a,b,c,t,g,e=1;

 scanf("%d",&t);

 while(t--){

  scanf("%d %d %d",&a,&b,&c);

  g=gcd(abs(a),abs(b));

  if(c%g==0)

   printf("Case %d: Yes\n",e);

  else 

   printf("Case %d: No\n",e);

   e++;

 }

 return 0;

}



Saturday, 18 July 2015

CANDY

Candy I

Link to the question : CANDY 

HINT :

Its obvious that if the number of chocolates is a multiple of the number of students then its possible to distribute equally among them. Now to count the number of moves so that each child get gets equal number of chocolates, we run a loop and all subtract all elements lesser than the average with the average. The summation of this will give us the number of moves.

RECOMMENDED QUESTION :

 Try solving this question related to LCM. 

SOURCE CODE :

#include<iostream>
using namespace std;
int main()
{
    int n=1,i;
    while(n!=-1)
    {
        cin>>n;
        if(n!=-1)
        {
            int a[n],s=0;
            for(i=0;i<n;i++)
            {
                cin>>a[i];
                s=s+a[i];
            }
            if(s%n!=0)
                cout<<"-1"<<endl;
            else
            {
                s=s/n;
                int m=0;
                for(i=0;i<n;i++)
                {
                    if(a[i]<s)
                        m=m+s-a[i];

                }
                cout<<m<<endl;
            }
        }
    }
    return 0;
}

BWIDOW

Black Widow Rings

Link to the question : BWIDOW 

HINT :

The question statement is very much clear, and even you need to solve it using the basic approach. Just compare the ring with the maximum inner radius with the outer radius of other rings. 

RECOMMENDED QUESTION :

Try this question on gcd after this one.

SOURCE CODE :

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int n;
        scanf("%d",&n);
        long long int a[n][2],max=0,max2=0;
        int i,r,rr;
        for(i=0;i<n;i++)
        {
            scanf("%lld%lld",&a[i][0],&a[i][1]);
            if(a[i][0] > max)
            {
                max = a[i][0];
                r= i;
            }
            else if(a[i][1]>max2)
            {
                max2= a[i][1];

            }
        }

        if(max>max2)
            printf("%d\n",r+1);

        else
            printf("-1\n");
    }
    return 0;
}
 

BOMARBLE

D - Playing with Marbles

Link to the question : BOMARBLE 

HINT :

Simple formula to get accepted. We need to solve it using recursion. Observe the sample input and the output and try to get a recursive relation among them. If still stuck, check the source code.

RECOMMENDED QUESTION :

Try your hands on this ad-hoc question .

SOURCE CODE :

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

long long int rec(int m)
{
    if(m==1)
        return 5;
    else if(m==2)
        return 12;
    else
        return rec(m-1) + 10 + (m-3)*3;
}

int main()
{
    int n,x=1;;
    long long int s;


    while(x!=0)
    {
           scanf("%d",&n);
           x=n;
           if(x!=0)
           {
               s = rec(x);
               printf("%lld\n",s);
           }


    }
    return 0;

}

ATOMS

Atoms in the Lab

Link to the question : ATOMS 

HINT : 

At every second,a single atom breaks  into k atoms. Hence if there are n atoms at time t, then at time t+1 there will be n*k atoms. Since, there is a limit given to the maximum number of atoms m, we need to check if n*k is smaller than m and thereby also keep counting the time.

RECOMMENDED QUESTION :

Try this very simple question after getting an AC in this one.

SOURCE CODE : 

#include<stdio.h>
int main()
{
    int p;
    scanf("%d",&p);
    while(p--)
    {
        unsigned long long int n,k,m,s=1,t=0;
        scanf("%llu%llu%llu",&n,&k,&m);
        if(n>m)
            printf("0\n");
        else
        {
            while(s<=m/n)
            {
                s=s*k;
                if(s<=m/n)
                    t++;
            }
            printf("%llu\n",t);
        }
    }
    return 0;
}

Wednesday, 1 July 2015

ABSP1

Abs(a-b) I

Link to the question: ABSP1 

HINT :

You must have already noticed that the number are in sorted order. If you do some rough work for input of 5-6 numbers and form pairs as given in the question and subtract you will notice that each element in the i-th position is added i-th times and subtracted n-1-i th times. Using this you can write your code.

SOURCE CODE :

/* ABSP1 */ /* Sushant Gupta */
#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i;
        scanf("%d",&n);
        long long int a[n],s=0;
        for(i=0;i<n;i++)
            scanf("%lld",&a[i]);
        for(i=n-1;i>=0;i--)
            s= s+ (i*a[i]) - a[i]*(n-1-i);
        printf("%lld\n",s);
    }
    return 0;
}

Tuesday, 30 June 2015

STREETR

STREET TREES 

Link to the question: STREETR

HOW TO APPROACH:

The question asks us to find the minimum number of trees which must be planted so that the distance between the adjacent trees is same. Hence we find the gcd of the difference between the adjacent trees, and then divide the difference with the gcd and it.

RECOMMENDED QUESTION :

Try this very easy question.

SOLUTION :

/* Street Trees (STREETR) */
/* -Susahnt Gupta */

#include<stdio.h>
long long int gcd(long long int a, long long int b)
{
    if(b>a)
        return gcd(b,a);
     else if(b!=0)
            return gcd(b,a%b);
    else
        return a;
}

int main()
{
    int t,i;
    scanf("%d",&t);
    long long int a[t],h,diff,s=0;
    for(i=0;i<t;i++)
        scanf("%lld",&a[i]);
     h= a[1]-a[0];
    for(i=1;i<t;i++)
    {
        diff= a[i]- a[i-1];
        h= gcd(diff,h);
    }

    for(i=1;i<t;i++)
    {
        diff= a[i]- a[i-1];
        s= s+ (diff/h)- 1;
    }
    printf("%lld\n",s);
    return 0;
}