Hackerrank 101oct Halloween party

PROBLEM STATEMENT:

You are given a very large rectangular chocolate bar. You must cut only as 1 x 1 pieces. You are allowed to do only ‘K’ cuts at the maximum. What is maximum number of chocolate pieces can u cut ?

For a detailed problem statement click here

SOLUTION:

Let ‘a’ be the number of cuts made horizontally. Let ‘b’ be the number cuts vertically.

Total number of Chocolate pieces  = a * b

We know that, b = K – a;

So, Tot. no. of Choc. Pieces = a * K – a

Only When a = K / 2, No. of Chocolate pieces will be maximum

So, Maximum No. of Pieces = K2 /2  – K2/4   =   K2 / 4

CODE:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {

int t;
scanf("%d",&t);
while(t--)
{
          long long int k;
          scanf("%lld",&k);
          printf("%lld\n", (k*k)/4LL);
}
return 0;
}
Advertisements

3 thoughts on “Hackerrank 101oct Halloween party

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s