## Algorithm

Problem Name: Mathematics - Halloween party

In this HackerRank in Mathematics - Halloween party solutions,

Alex is attending a Halloween party with his girlfriend, Silvia. At the party, Silvia spots the corner of an infinite chocolate bar (two dimensional, infinitely long in width and length).

If the chocolate can be served only as 1 x 1 sized pieces and Alex can cut the chocolate bar exactly K times, what is the maximum number of chocolate pieces Alex can cut and give Silvia?

Input Format
The first line contains an integer T, the number of test cases. T lines follow.
Each line contains an integer K.

Output Format

T lines; each line should contain an integer that denotes the maximum number of pieces that can be obtained for each test case.
Constraints

1 <= T <= 10

2 <= K <= 107

Note: Chocolate must be served in 1 x 1 sized pieces. Alex can't relocate any of the pieces, nor can he place any piece on top of another.

Sample Input #00

4
5
6
7
8


Sample Output #00

6
9
12
16


Explanation
The explanation below is for the first two test cases. The rest of them follow a similar logic.

For the first test-case where K = 5, you need 3 horizontal and 2 vertical cuts.

For the second test case, where K = 6 you need horizontal and 3 vertical cuts.

## Code Examples

### #1 Code Example with C Programming

Code - C Programming


#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* ltrim(char*);
char* rtrim(char*);

int parse_int(char*);

/*
* Complete the 'halloweenParty' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts INTEGER k as parameter.
*/

long halloweenParty(int k) {
long fin;
if(k%2==0)
fin=(long)(k/2)*(k/2);
else
fin=(long)((k/2)+1)*(k/2);
return fin;
}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

for (int t_itr = 0; t_itr < t; t_itr++) {

long result = halloweenParty(k);

fprintf(fptr, "%ld\n", result);
}

fclose(fptr);

return 0;
}

size_t alloc_length = 1024;
size_t data_length = 0;

char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) {
break;
}

data_length += strlen(cursor);

if (data_length  <  alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}

alloc_length <<= 1;

data = realloc(data, alloc_length);

if (!data) {
data = '\0';

break;
}
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';

data = realloc(data, data_length);

if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);

if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}

return data;
}

char* ltrim(char* str) {
if (!str) {
return '\0';
}

if (!*str) {
return str;
}

while (*str != '\0' && isspace(*str)) {
str++;
}

return str;
}

char* rtrim(char* str) {
if (!str) {
return '\0';
}

if (!*str) {
return str;
}

char* end = str + strlen(str> - 1;

while (end >= str && isspace(*end)) {
end--;
}

*(end + 1) = '\0';

return str;
}

int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);

if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}

return value;
}

Copy The Code &

### #2 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);

/*
* Complete the 'halloweenParty' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts INTEGER k as parameter.
*/

long halloweenParty(int k) {
long x = (long)k;
// be careful with the type
return (x/2)*(x-x/2);
// return (k/2)*(k-k/2); immediately will cause int overflow
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string t_temp;
getline(cin, t_temp);

int t = stoi(ltrim(rtrim(t_temp)));

for (int t_itr = 0; t_itr  <  t; t_itr++) {
string k_temp;
getline(cin, k_temp);

int k = stoi(ltrim(rtrim(k_temp)));

long result = halloweenParty(k);

fout << result << "\n";
}

fout.close();

return 0;
}

string ltrim(const string &str) {
string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun < int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun < int, int>(isspace))).base(),
s.end()
);

return s;
}

Copy The Code &

### #3 Code Example with C# Programming

Code - C# Programming


using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{

/*
* Complete the 'halloweenParty' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts INTEGER k as parameter.
*/

public static long halloweenParty(int k)
{
return (long)Math.Floor((decimal)k/2) * (long)Math.Ceiling((decimal)k/2);
}

}

class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

for (int tItr = 0; tItr  <  t; tItr++)
{

long result = Result.halloweenParty(k);

textWriter.WriteLine(result);
}

textWriter.Flush();
textWriter.Close();
}
}

Copy The Code &

### #4 Code Example with Java Programming

Code - Java Programming


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

/*
* Complete the 'halloweenParty' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts INTEGER k as parameter.
*/

public static long halloweenParty(int k) {
return (long)(Math.ceil((double)k/2)*(k/2));
}

}

public class Solution {
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

IntStream.range(0, t).forEach(tItr -> {
try {

long result = Result.halloweenParty(k);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});

bufferedWriter.close();
}
}

Copy The Code &

### #5 Code Example with Javascript Programming

Code - Javascript Programming


'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});

process.stdin.on('end', function() {
inputString = inputString.split('\n');

main();
});

return inputString[currentLine++];
}

/*
* Complete the 'halloweenParty' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts INTEGER k as parameter.
*/

function halloweenParty(k) {
return Math.ceil(k / 2) * Math.floor(k / 2)

}

function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

for (let tItr = 0; tItr  <  t; tItr++) {

const result = halloweenParty(k);

ws.write(result + '\n');
}

ws.end();
}

Copy The Code &

### #6 Code Example with Python Programming

Code - Python Programming


#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'halloweenParty' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts INTEGER k as parameter.
#

def halloweenParty(k):
if k % 2 == 0:
x = k//2
y = x
return x*y
else:
x = (k/2)+0.5
y = k-x
return int(x*y)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

t = int(input().strip())

for t_itr in range(t):
k = int(input().strip())

result = halloweenParty(k)

fptr.write(str(result) + '\n')

fptr.close()

Copy The Code &