Thursday 30 July 2015

HOTELS


Hotels Along the Croatian Coast

Link to the question :  HOTELS

HINT :

We apply the sliding concept. We start calculating the sum from index 0 of the array. If the sum exceeds the maximum amount given, we start subtracting  from sum from the beginning of the array, untill the sum becomes less than or equal to maximum amount.

SOURCE  CODE :


#include<stdio.h>
int main()
{
    int i, l;
    long long n, m, sum, max_sum;

    scanf("%lld%lld",&n, &m);

         int a[n];
        for(i=1; i<=n; i++)
        {
             scanf("%d", &a[i]);
        }
        l=1;
        max_sum = 0;
        sum = 0;
        for(i=1; i<=n; i++)
        {
            sum = sum + a[i];
            while(sum>m)
            {
                sum = sum - a[l];
                l++;
                if(max_sum<=sum && sum<=m)
                {
                    max_sum = sum;

                    break;
                }

            }
            if(max_sum<=sum && sum<=m)
            {
                max_sum = sum;

            }
        }
        printf("%lld\n", max_sum);

    return 0;
}

HLP_RAMS


Topper Rama Rao

Link to the question : HLP_RAMS 

HINT :

To find whether nCr is odd or even we use the lucas theorem. 
Lucas theorem states that nCr % m, where, m is a prime number = (n0 C r0 % m) * (n1 C r1 % m)*........
(nk C rk % m), where n<0n1...nk and r0r1...rk are the representation of n and r in base m respectively. Hence, in base 2 representation, the digits will be either 0 or 1. 
So,according to lucas theorem,  nCr % 2 will give us the cases 1C1, 1C0, 0C0, 0C1. Now, 0C1  = 0. Hence, if nCr has the case 0C1 then its always even else odd. 
Now for counting the number of odd terms we count the number of 1's in n' s binary representation. We can assign either 0 or 1 to the 1's of n in nCr. Hence, number off odd terms = 2^(no of 1's).

SOURCE CODE :

/* Topper Rama Rao */
/* Sushant Gupta */
#include<stdio.h>
#include<math.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long int n,i,odd,x,c=0;;
        scanf("%lld",&n);
        x=n;
        if(n==0)
            printf("0 1\n");
        else
        {
            while(n>0) {
                if(n&1==1)
                    c++;
               n=  n>>1;
            }
            odd = pow(2,c);
            printf("%lld %lld\n",x+1-odd,odd);


        }


    }
    return 0;
}

Wednesday 29 July 2015

HEADSHOT

Headshot

Link to the question : HEADSHOT 

HINT :

The question requires a bit of your probability skills. Use the greedy approach. And if still not able to derive the formula, try to grasp it up with the source code.

SOURCE CODE :

/* Headshot */
/* Sushant Gupta */
#include<stdio.h>
#include<string.h>
int main()
{
    char a[150];
    int i,n,w=0,b=0;
    float rot,sht;
    scanf("%s",a);
    n = strlen(a);
    for(i=0;i<n;i++)
    {
        if(a[i]=='0')
            w++;


    }
    rot = (float)w/n;
    for(i=0;i<n-1;i++)
    {
        if(a[i]=='0' && a[i+1]=='0')
            b++;
    }
    if(a[0]=='0' && a[n-1]=='0')
        b++;
    sht = (float)b/w;

    if(rot == sht)
        printf("EQUAL\n");
    else if(rot>sht)
        printf("ROTATE\n");
    else
        printf("SHOOT\n");
    return 0;
}

HC


Happy Coins

Link to the question : HC

HINT :

You can ignore the word consecutive. Though I wont say it misleading, but not taking that into consideration will make the problem solving a bit more simple. Try some cases and even if you cant check the source code.

SOURCE CODE :


#include<stdio.h>

#include<string.h>

int main()

{

long int t,i,n,count=0;

char s[4],a[]="lxh";

scanf("%ld",&t);

while(t--)

{

    count=0;

    scanf("%ld",&n);

    while(n--)

    {

        scanf("%s",s);

        if(strcmp(s,a)==0)

            count++;

    }

    if(count%2==0)

        printf("hhb\n");

    else

        printf("lxh\n");



}

return 0;



}

RECOMMENDED QUESTION :

After solving this question, I would like you to try your hands out in this question . 

Monday 27 July 2015

HANGOVER

Hangover

 Link to the question : HANGOVER

HINT :

Just simply find the sum of the series given in the question : 1/2 + 1/3 +...1/k. till the sum is less than equal to the input, n. Keep a count  of the k.

RECOMMENDED QUESTION :

Try solving this question after completing this.

SOURCE CODE :

#include<stdio.h>
int main()
{
    float s,n,x=1,i;
    while(x!=0)
    {
        scanf("%f",&n);
        x=n;
        if(x!=0)
        {
            s=0;i=2;
            while(s<=n)
            {
                s=s+1/i;

                i++;
            }
            printf("%1.0f card(s)\n",i-2);
        }
    }
    return 0;
}

GUESSTHE

Guess the Number

Link to the question : GUESSTHE 

HINT :

We need to find the LCM of the numbers which are given a Y, and then check for the numbers which are given N, whether they divide the LCM.

RECOMMENNDED QUESTION:

You would surely love solving this question  after  trying your hands in this one.

SOURCE CODE :

#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 a;
    else
        return gcd(b,a%b);
}
long long int lcm(long long int a,long long int b)
{
    return a*b/gcd(a,b);
}
int main()
{
    char c,a[22];

    while(1) {
            int i=1,j=0;
            long long int k=1;
            scanf("%c",&c);
            while(c!='\n'&&c!='*')
           {



                      if(c=='Y')
                          k= lcm(k,i);
                     else if(c=='N')
                           a[j++]= i;
                     i++;
                     scanf("%c",&c);


           }
           if(c=='*')
            return 0;
            else {
           for(i=0;i<j;i++)
           {
               if(k%a[i]==0)
               {
                   k=-1;
                   break;
               }
           }
           printf("%lld\n",k);
            }
    }

}
 

Sunday 26 July 2015

GSHOP


Rama and Friends

Link to the question : GSHOP 

HINT :

We must keep in find the following cases and then solve.
  • If number of times we can execute the operation, k, is lesser than the number of negative numbers then we simply multiply the larger negative numbers with -1 and add.
  • Else, we multiply all negative numbers with -1 and :
                     1. If number of times we execute the operation is even, simply add all the numbers.
                     2. Else multiply the smallest number with -1 and the add.

RECOMMENDED QUESTION :

I would like the reader to solve this question after getting an AC in this one !!

SOURCE CODE :

#include <stdio.h>


int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  int n,k;
  scanf("%d%d",&n,&k);
  int count_n=0,count_z=0,count_p=0,i;
  long long sum=0,min=9999999,temp;
  int a[n+9];
  for(i=0;i<n;i++){
   scanf("%d",&a[i]);
   temp=a[i]>0?a[i]:-1*a[i];
   if(temp<min)
    min=temp;
   if(a[i]<0)
    count_n++;
  }
  if(k<=count_n)
  {
   int j=0;
   for(i=0;i<n;i++)
    if(a[i]<0 && j<k)
    {
     a[i]=-a[i];
     j++;
    }
   for(i=0;i<n;i++)
    sum+=a[i];
   printf("%lld\n",sum);
  }
  else
  {
   for(i=0;i<count_n;i++)
    a[i]=-a[i];
   for(i=0;i<n;i++){
    sum+=a[i];
   }
   if((k-count_n)%2!=0)
    sum+=-(2*min);
   printf("%lld\n",sum);
  }
 }
 return 0;
}

GIRLSNBS

Girls and Boys

Link to the question : GIRLSNBS 

HINT :

Let g be the number of girls and b be the number of boys and let b>g. Now, if * represents the girls and let their arrangement be like :
      *     *      *      *        *   , then think how many boys will you fill on those spaces so that you get the minimum gender regularity.There are g+1 spaces. So first we fit all the spaces with equal number of boys and then distribute the remaining boys to one boy per space.

RECOMMENDED QUESTION :

I would like my readers to solve this question after trying this one.

SOURCE  CODE :

#include<stdio.h>

int main()

{

    int g,b,x=1,y=1;

    while(x!= -1 && y!=-1)

    {

                scanf("%d%d",&g,&b);

                x=g; y=b;

                if(x!=-1 && y!=-1)

                {

                    if(g==b){

                            if(g==0)

                               printf("0\n");

                       else

                        printf("1\n");

                    }

                    else

                    {

                        if(g>b)

                        {

                            x=g; y=b;

                        }

                        else{ x=b; y=g;}

                        if((x%(y+1))==0)

                            printf("%d\n",(x/(y+1)));



                        else

                            printf("%d\n",(x/(y+1))+1);

                    }

                }

    }

    return 0;

}
    

Saturday 25 July 2015

FUNPROB

Yanu in Movie theatre

Link to the question : FUNPROB 

RECOMMENDED QUESTION :

Try solving this question after solving this.

SOURCE CODE :

#include<stdio.h>
int main()
{
     int m=1,n=1;
     double x,y;
     while(m!=0 && n!=0)
     {
         scanf("%d%d",&n,&m);
         if(n!=0 && m!=0)
         {
             if(n>m || m==0)
                printf("0.000000\n");
             else if(n==0)
                printf("1.000000\n");
             else
             {
                 x=m;
                 y=n;
                 printf("%.6lf\n",(x-y+1.0)/(x+1.0));
             }

         }
     }
     return 0;
}

FENCE1

Build a Fence

Link to the question : FENCE1 

HINT : 

Imagine a very long straight ruler and a piece of smallish rope. The rope is tied to the ruler at one end and loose at the other. Now connect the other end of the rope to the ruler and you'd get a shape with a straight bottom (made of the ruler itself) and a curvy top (made by the rope). This shape has an area, what is its maximum area given the length of the rope?

RECOMMENDED QUESTION:

Try your hands in this question after getting an AC in this.

SOURCE CODE :

#include<stdio.h>

int main()
{
    int l,x=1;
    double s;
    while(x!=0)
    {
        scanf("%d",&l);
        x=l;
        if(x!=0)
        {
            s= (float)(l*l/(2*3.1415926 ));
            printf("%.2f\n",s);
        }
    }



    return 0;
}

FCTRL2

Small factorials

Link to the quesstion : FCTRL2 

HINT :

Since factorials of numbers like 100 will be very long, almost 160 digits. So we need to store the result in an array. Check the code on how to implement it or you can also read its tutorial in codechef.

RECOMMENDED QUESTION :

Try solving this question .

SOURCE CODE :


#include<stdio.h>

#include<stdlib.h>



void fact(int num,int a[],int n)

{

     int temp=0,x=0,i=0,j,k;

     for(j=num-1;j>0;j--)

     {    i=0;

         for(k=n;k>0;k--)

        {



              x= a[i]*j + temp;

              a[i]= x%10;

              temp= x/10;



               i++;





        }



        while(temp!=0)

        {

           a[i]=temp%10;

          temp=temp/10;



           i++;

           n=i;

        }



     }



     for(j=n-1;j>=0;j--)

        printf("%d",a[j]);

        printf("\n");



}



int main()

{

    int t;

    scanf("%d",&t);

    while(t--)

   {   int a[200]={0};

       int n;

       scanf("%d",&n);

       int n2=n; int i=0;

       while(n>0)

       {

           a[i]=n%10;

           n=n/10;



           i=i+1;

       }

       int k=i;



       fact(n2,a,k);

   }

   return 0;





}

FCTRL


Factorial

Link to the question : FCTRL 

HINT :

For finding the number of trailing zeroes, we need to find how many factors of 10 are present. Since 10 = 2 *5 , we basically need to find the number of factors of 5, since its obvious that factors of 2 will be greater in number.

RECOMMENDED QUESTION :

Try this question afer completing this.

SOURCE CODE :


#include<stdio.h>

    #include<math.h>



    int main()

    {

      int t,c,i;

      long n;

      scanf("%d",&t);

      while(t--)

      {

          c=0;i=1;

          scanf("%ld",&n);

          while(n/pow(5,i)>=1)

          {

              c=c+(int)(n/pow(5,i));

              i=i+1;

          }

          printf("%d\n",c);

      }

       return 0;

    }

Friday 24 July 2015

FAVDICE


Favorite Dice

Link to the question : FAVDICE 

HINT :

Read some articels on : Expected value. Understand it properly and then implement the formula.

SOURCE CODE :

#include<stdio.h>

int main()
{
    int t,n,i;
    float s;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        s=0;
        for(i=1;i<=n;i++)
            s= s+ (float)1/(i);
        s=(float)(s*n);
        printf("%.2f\n",s);
    }



    return 0;
}



RECOMMENDED QUESTION :

I would like my reader to solve this question after this one.

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;
}

FARIDA



Princess Farida

Link to the question : FARIDA 

HINT :

The question asks us to take the maximum number of gold coins from the monsters such that if m_1, m_2, m_3......m_n are the monsters then  you can take gold coin from monster m_i only if you had not taken from m_(i-1).
This can be solved very easily by dp. Let us take an array b[] such that b[i] stores the maximum number of gold coins you can get from m_1 to m_i monsters. So,
b[i] = max( gold coins taken from ith monster + b[j]) where 0<j<i-1 .

RECOMMENDED QUESTION :

After solving this dp question, you would love solving this question as well.

SOURCE CODE :


/* Princess Farida */

/* Sushant Gupta */



#include<iostream>

using namespace std;

int main()

{

    int t,k=1;

    cin>>t;

    while(t--)

    {

        int n,i,j;

        cin>>n;

        long long int a[n],b[n],max=0;



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

        {

            cin>>a[i];

            b[i]= 0;

        }

        b[0]= a[0];

        b[1]= a[1];

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

        {

            for(j=0;j<i-1;j++)

            {

                if(b[i]< b[j] + a[i])

                    b[i]= b[j] + a[i];

            }

        }

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

        {

            if(max< b[i])

                max = b[i];

        }

        cout<<"Case "<<k++<<": "<<max<<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;
    }
}
 

Thursday 23 July 2015

FANCY



FANCY NUMBERS

Link to the question : FANCY 

HINT :

A very good question on permutation and combination. 
If we take for example the test case given in the question : 11112
The answer depends on the number of possible combinations of 1111 which satisfies the rules of the question.
Hence the number of combinations of 1111 is 2^3. As our combinations will depend on whether we choose a particular 1.

RECOMMENDED QUESTION :

After solving this pnc question, you may like solving this adhoc question .

SOURCE CODE :

#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char a[50];
        int i,c,j;
        long long int ans=1;
        scanf("%s",a);
        i=0;
        j=strlen(a);

        while(i<j)
        {    c=0;

            while(a[i+1]==a[i])
            {
                c++;
                i++;
            }
            ans=ans * pow(2,c);
            i++;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
 

ENIGMATH


PLAY WITH MATH

Link to the question : ENIGMATH 

HINT :

A question similar to CEQU. 
In order for x and y to satisfy the equation a*x = b*y , a*x should be equal to the lcm of (a,b). Similarly, b*y should also be equal to the lcm of the coefficients.

RECOMMENDED QUESTION :

A similar question to this : CEQU.
You will surely enjoy solving it.

SOURCE CODE :

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

int main()
{
    int t;
    long long int a,s,b,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
        while(a%2==0 && b%2==0)
        {
            a=a/2;
            b=b/2;
        }
        if(a>b)
            s=b;
        else
            s=a;
        for(i=3;i<=sqrt(s);i=i+2)
        {
            while(a%i==0 && b%i==0)
            {
                a=a/i;
                b=b/i;
            }
        }
        printf("%lld %lld\n",b,a);
    }
    return 0;
}

EIGHTS


Triple Fat Ladies

Link to the question : EIGHTS 

HINT :

No need to hard code. Just take out your calculator and check for the first few numbers whose cube ends with 888 and observe the pattern. 

Still cant get the pattern?
Here it is : 
1 - 192
2 - 442
3 - 692
4 - 942

RECOMMENDED  QUESTION :

Try solving this question.

SOURCE CODE :

#include<iostream>


using namespace std;

int main()

{

    long long k,t;

    cin>>t;

    while(t--)

    {

        cin>>k;

        cout<<192 + (k-1)*250<<endl;





    }

    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;

}



    


DANGER


In Danger

Link to the question : DANGER 

HINT :

Josephus problem can be solved by using a circular queue but for k=2 we can derive a formula. First try solving by keeping number of people, n, equal to some power of 2, i.e, 4,8 etc. You will notice that the last to stay is at position 1. 
Hence we write  n = 2^m + t. Now, if t people are killed, the one with whom the cycle starts stays till the end. So, after killing t people, the cycle will start from the position 2*t - 1. Hence, you need to find that position.

RECOMMENDED QUESTION :

After getting an AC, try solving this question .

SOURCE CODE :

/* In Danger */

/* Sushant Gupta */



#include<stdio.h>

#include<math.h>

int main()

{

    char a[10];

    scanf("%s",a);



    while(1)

    {

        if(a[0]== '0' && a[1]== '0')

            return 0;



        long long int x;

        x = 10 * (a[0] - '0') + (a[1]- '0');

        x = x * pow(10, (a[3]- '0'));

        int m;

        m = log2(x);

        long long int n;

        n = pow(2,m);

        long long int ans;

        ans = 2*(x - n) + 1;

        printf("%lld\n",ans);

        scanf("%s",a);

    }



}

CUTCAKE


Eat all the brownies !

Link to the question : CUTCAKE 

HINT :

A good mathematical question. You can  approach to the solution of the question by thinking it this way - how will n lines cut a plane so that you get maximum pieces. Many of you might have solved this question during your entrance exam preparation. If not here's the solution:
1 line divides a plane into 2 parts.
2 lines divide it into 4 and similarly 3 lines divide it into 7.
If you go further, you will see that the differences of these numbers are in AP. Hence, derive the formula and solve it accordingly.

RECOMMENDED QUESTION :

You will love solving this adhoc question . 

SOURCE CODE :

#include<stdio.h>

#include<math.h>

int main()

{

      long long int n,tn;

      int t;

      scanf("%d",&t);

      while(t--)

      {

          scanf("%lld",&tn);

          if(tn==1)

            printf("0\n");



          else  {

          n= (1 + sqrt(1+ 8*(tn-1)));

          n=n/2;

          printf("%lld\n",n-1);

      } }

      return 0;



}

    

Tuesday 21 July 2015

CRDS



Cards

Link to the question : CRDS 

HINT :

A very simple question. Just observe the pattern and derive a formula.

RECOMMENDED QUESTION :

Try this adhoc question .

SOURCE CODE :

#include<iostream>

using namespace std;

int main()

{

    long long t,n,s;

    cin>>t;



    while(t--)

    {

        cin>>n;

        s= (2*n*(n+1)/2) + (n*(n-1)/2);

        cout<<s%1000007<<endl;

    }



    return 0;

}

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;

}



CANDY3

Candy III

Link to the question : CANDY3 

HINT :

The question sounds very easy. And yes it is quite simple. Just be careful about the large input size and where to use the modulo function.

RECOMMENDED QUESTION :

I think you will like solving this question involving gcd.

SOURCE CODE : 

#include<iostream>
using namespace std;
int main()
{
    long long t,n,s,*a,i;
    cin>>t;
    cout<<endl;
    while(t--)
    {
        cin>>n;
        a= new (nothrow)long long[n];
        s=0;
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            s=(s+a[i])%n;
        }
        if(s%n==0)
            cout<<"YES"<<endl<<endl;
        else
            cout<<"NO"<<endl<<endl;

    }
    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;

}

BINSTIRL

Binary Stirling Numbers

Link to the question : BINSTIRL 

HINT :

Read the article on wikipedia on binary stirling numbers. There you will also find a formula  to check the pairity of them.

RECOMMENDED QUESTION :

Try this dp question .

SOURCE CODE :

#include <stdio.h>
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
    long long int n,m;
scanf("%lld%lldd", &n, &m);
printf("%d\n", !((n-m)&((m-1)>>1)));
}
return 0;
}

BEENUMS


Beehive Numbers

Link to the question : BEENUMS 

HINT :

The question may be a bit lengthy and also it might be difficult for us to imagine a beehive following the norms as per the question. But we dont have to worry about that. All we need to observe is the pattern in given example and VOILA our question is solved. 


RECOMMENDED QUESTION :

Try your hands in this question.

SOURCE CODE :

#include<stdio.h>
#include<math.h>
int main()
{
    long long int n,x=1,y;
    double t;
    while(x!=-1)
    {
        scanf("%lld",&n);
        x=n;
        if(x!=-1)
        {
            if(n%6==1)
            {
                  t= sqrt(1+ (4*(n-1)/3));
                  y= (int)(t*10);
                  if(y==t*10)
                    printf("Y\n");
                  else
                    printf("N\n");
            }
            else
            printf("N\n");
        }
    }
    return 0;
}