Showing posts with label simple. Show all posts
Showing posts with label simple. Show all posts

Wednesday, 12 August 2015

MANGOES


Real Mangoes for Ranjith

Link to the question : MANGOES 

HINT :

The question might look lengthy and you may think implementing the algorithm will  be a tough task, but a little careful observation may make this question look very simple. As per the question mangoes are real if GCD of each pair of the  set  {mi, mi+1, mi+2} equals 1. Now this is only possible when mi and (mi + 2) are odd else if they are even, they will have at least 2 as their GCD. Hence all you need to is find the sum of the odd terms.

SOURCE CODE :

 #include<stdio.h>
int main()
{
    int t;
    long long int n,x,s;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        x=n;
        if(n%2==0)
            n=(n-2)/2;
        else
            n=(n-1)/2;

        s=(n*n)%x;
        printf("%lld\n",s);

    }
    return 0;
}
 

Friday, 7 August 2015

LENGFACT

Factorial length

Link to the question : LENGFACT 

HINT :

Apply the kamenetsky formula.

SOURCE CODE :



#include<stdio.h>
#include<math.h>
int main()
{
int t;
long long int  ans,n;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%lld",&n);
if(n<3)
printf("1\n");
else{
ans=ceil(log10(2*3.141592653589793*n)/2 + n*log10(n/2.7182818284590452353));
printf("%lld\n",ans);
}
}
return 0;
}

Thursday, 6 August 2015

IITKWPCB

Check the coprimeness

Link to the question : IITKWPCB 

HINT :

Observe the test cases and derive the formula.

SOURCE CODE :

#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
    long long int n,c;
    scanf("%lld",&n);
    if(n%2==0)
    {
        c=n/2 - 1;
        if(c%2==0)
            printf("%lld\n",c-1);
        else
            printf("%lld\n",c);
    }
    else
    {
        printf("%lld\n",n/2);
    }
}
return 0;
}
 

HUBULLU

Link to the question : HUBULLU

HINT :

Observe carefully. Try some simple cases by taking n a  small number and then you will get the answer.

SOURCE CODE :


/* Hubulullu */
/* sushant gupta */

#include<iostream>

using namespace std;
int main()
{
     int t,s;
     long long int n;
     cin>>t;
     while(t--)
     {
         cin>>n>>s;
         if(s==0)
            cout<<"Airborne wins."<<endl;
         else
            cout<<"Pagfloyd wins."<<endl;
     }
     return 0;
}

HPYNOS

Happy Numbers I

Link to the question : HPYNOS 

HINT :

Just simple brute force. Keep on breaking the number untill you get a single digit. If its 1 then its a happy number else not a happy number.

SOURCE CODE :

#include<stdio.h>



int main()
{
    char a[10];int i=0,b=0,x,c=1;
    gets(a);

    while(a[i]!='\0')
    {
            b= b + (a[i] - 48) * (a[i]-48);

            i++;
    }



    while(b>9)
    {     x=0;
        while(b>0)
        {    x=(b%10) * (b%10) +x;
             b=b/10;

        }
        b=x;
        c++;

    }
    if(b==1)
        printf("%d",c);
    else
        printf("-1");

    return 0;


}
 

Wednesday, 29 July 2015

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

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

Thursday, 23 July 2015

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;

}

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;

}

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

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

Friday, 17 July 2015

ARRAYSUB

SUBARRAYS

Link to the question : ARRAYSUB 

HINT :

I have applied the basic approach and it got accepted.  First I started a loop from 0 to [size - k]. And then for sub sequence of k elements, I printed the maximum element. This is done by brute force.

RECOMMENDED QUESTION :

Try your hands on this mathematical question .

SOURCE CODE :

#include<iostream>

using namespace std;
int main()
{
    int n,k,i,j,m=0;
    cin>>n;
    int a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    cin>>k;
    for(i=0;i<=n-k;i++)
    {
        m=a[i];
        for(j=1;j<k;j++)
        {
            if(a[i+j]>m)
                m=a[i+j];
        }
        cout<<m<<" ";

    }
}

ARMY


Army Strength

Link to the question :  ARMY

HINT :

The question may sound a little tricky or  difficult but actually its very simple. If you read it carefully, you will notice that all you need to do is sort both the armies and compare the last element of both the armies and then predict the result.

RECOMMENDED QUESTION :

Try your hands on this mathematical question.

SOURCE CODE :

#include<iostream>

#include<algorithm>

using namespace std;



int main()

{

    int t,ng,nm,i;

    cin>>t;

    cout<<endl;

    while(t--)

    {

        cin>>ng>>nm;

        int g[ng],m[nm];

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

            cin>>g[i];

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

            cin>>m[i];

        sort(g,g+ng);

        sort(m,m+nm);

        

            if(g[ng-1]>=m[nm-1])

            

                cout<<"Godzilla"<<endl;

                

            else

                cout<<"MechaGodzilla"<<endl;

        }
return 0;

}