Showing posts with label AP. Show all posts
Showing posts with label AP. Show all posts

Wednesday, 22 July 2015

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;



}

    

Friday, 17 July 2015

AP3

AP - Complete The Series v2

 Link to the question : AP3

HINT :

Again a simple question based on the arithmetic progression. The problem becomes tricky if not taken care of the precisions.

RECOMMENDED QUESTION :

Try your hands on this question .

SOURCE CODE :

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

int main()
{
    int t; scanf("%d",&t);
    while(t--)
    {
       long long int a3,a3l,s,i,a,d,len; 
          
      long double n,sq,diff;
      scanf("%lld",&a3);
      scanf("%lld",&a3l);
      scanf("%lld",&s);
     
      sq=sqrtl(((5.0*a3l+7.0*a3+2.0*s)*(5.0*a3l+7.0*a3+2.0*s))-(48.0*(a3l+a3)*s));
      n=((5.0*a3l+7.0*a3+2.0*s)+sq)/(2.0*(a3l+a3));
     
      len=llrintl(n);
      printf("%lld\n",len);
     
      diff=(a3l-a3)/(len-6.0);
      d=llrintl(diff);
     
      a=(a3-(2*d));
       for(i=0;i<len;i++)
       {
                       printf("%lld ",a+i*d);
       }
        printf("\n");
      
      
       }  
     
     return 0;
}

Wednesday, 15 July 2015

AP2


AP - Complete The Series (Easy)

Link to the question : AP2 

HINT : 

Simply apply the formulae that you have learnt in your elementary school. A very easy question on arithmetic progression , even the question states it. But dont forget to simplify the equations a bit if you dont want your code to be  a bit messy.

RECOMMENDED QUESTION :

Try this gcd question .

SOURCE CODE :

#include<stdio.h>
int main()
{
    int t;
    long long int sum, n3,l3,a,d,i,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld %lld %lld",&n3,&l3,&sum);
        n= (2*sum)/(n3+l3);
        d= (l3-n3)/(n-5);
        a= n3 - 2*d;
        printf("%lld\n",n);
        for(i=1;i<=n;i++)
            {
                printf("%lld ",a);
                a= a+d;
            }
            printf("\n");
    }
    return 0;
}