Showing posts with label brute force. Show all posts
Showing posts with label brute force. Show all posts

Friday, 4 March 2016

PROFF

Professor Farouk Question


Link to the question : PROFF

HINT :

Just simple brute force.  Add one digit at a time from right to left and check if there is a carry. Keep in mind some corner cases. 

RECOMMENDED QUESTION :

Try your hands on this question : Headshot

SOLUTION :

/*  PROFF */
/* Sushant Gupta */
#include<stdio.h>
#include<string.h>
int main()
{
  
    long long int x1=1,x2=1,n1,n2;
    while(1)
    {
        scanf("%lld%lld",&x1,&x2);
        if(x1==0 && x2==0)
            return 0;
        else
        {
            n1 = x1;
            n2 = x2;
            int s=0,c=0;
            while(n1 || n2)
            {
                s = ((n1 %10) + (n2 %10) + s >=10);
                c = c+s;
                n1 = n1/10;
                n2 = n2/10;
                /*if(s>9)
                {
                    c++;
                    f=1;
                }
                else if(s==9)
                {
                    if(f==1)
                        c++;
                    else
                        f= 0;
                }
                else
                    f= 0; */
            }
            if(c==0)
                printf("No carry operation.\n");
            else if(c==1)
                printf("1 carry operation.\n");
            else
                printf("%d carry operations.\n",c);
        }
    }
}

Monday, 5 October 2015

TWOSQRS


Two squares or not two squares

Link to the question : TWOSQRS

HINT :

Logic seems very clear. Just we need check if the number can be represented as a sum of two squares or not. A little bit optimisation may be preferrable.

RECOMMENDED QUESTION :

Try solving this  question .

SOURCE CODE :

#include<stdio.h>

#include<math.h>

void twosq(long long int x)

{

    long long int i,j=0;

    i= sqrt(x);

    while(i>0) {

    if(j*j>x)

      {



        printf("No\n");

         break;

      }

    else if(i*i + j*j == x)

        {



         printf("Yes\n");

         break;

        }

    else if(i*i + j*j <x)

         j++;

    else

        i--;

    }





}

int main()

{

    int t;

    scanf("%d",&t);

    while(t--)

    {

        long long int n;

        scanf("%lld",&n);

        twosq(n);

    }

    return 0;



}

Tuesday, 25 August 2015

CRNVALEN


The Valentine Confession

Link to the question :  CRNVALEN

HINTS :

The question asks us to find out the number of girls who are double dating. First we take the input a[ ] of all the girls, say, n. Then we sort it. Now, if all the elements of a[ ] are equal and and equal to n-1, then all the girls are double dating. This can be very easily imagined.
If the last element of the array  is greater than or equal to n, then the output is -1 as this is not possible.
Now if both the cases are not true, then the answer will be the last element of the array or the maximum number. But we need to check again if the girls are fooling. So for that we can keep a counter such that if c is the number of girls double dating then, c girls will say c-1 as their response and n-c girls will give c girls as their answer. Hence, c is the output.

SOURCE CODE :


/* The Valentine Confession */
/* Sushant Gupta */



#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
     int t;
     scanf("%d",&t);
     while(t--)
     {


    int n,f=0;
    scanf("%d",&n);
    long long int  i,ans,a[n],c1=0,c2=0;
    for(i=0;i<n;i++)
        scanf("%lld",&a[i]);
    sort(a,a+n);
    if(a[n-1] > n-1)
    {

          //printf("-1");
            f= 0;
    }
    else if(a[n-1] == a[0] && a[0]== n-1)
    {
        //printf("%d",n);
        ans = n;
        f = 1;
    }
    else {
            ans = a[n-1];
            c1 = ans;
            c2 = n - ans;
          for(i=n-1;i>=0;i--)
          {
                         if(a[i] == ans)
                            c2--;
                         else if(a[i] == ans-1)
                            c1--;
          }
          if(c1 ==c2  && c2 == 0 )
          {
              // printf("%d",ans);
              f = 1;
          }
          else
          {
              //printf("-1");
              f =0;
          }
    }
    if(f==0)
        printf("-1\n");
    else
        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;
}
 

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;


}
 

Monday, 27 July 2015

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

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

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

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

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

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

    }
}

Saturday, 4 July 2015

ACPC11B

Between the Mountains

Link to the question : ACPC11B

HINT :

The question asks us to find the minimum difference between two mountains. This can be done with the naive solution, i.e, checking each element with every other element. The time limit allows us to do this.

RECOMMENDED QUESTION :

Try this question .

SOURCE CODE :

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int a[n],i;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        int m;
        scanf("%d",&m);
        int b[m];
        for(i=0;i<m;i++)
            scanf("%d",&b[i]);
        int diff=1000000,s,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i]>b[j])
                    s= a[i]-b[j];
                else
                    s= b[j]-a[i];
                if(s<diff)
                    diff=s;


            }
        }
        printf("%d\n",diff);
    }
    return 0;
}

Tuesday, 30 June 2015

COMDIV


Number Of Common Divisors

Link to the question: COMDIV

HOW TO APPROACH:

The problem asks us to find the number of common divisors between two numbers. So its very obvious that the number which is formed by the common divisors of both the numbers is their gcd.

  RECOMMENDED QUESTION :

Try this sorting question after this one.

 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;

}