## Algorithm

Problem Name: Mathematics - Army Game

In this HackerRank in Mathematics - Army Game solutions,

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
#include <stdio.h>
int main ()
{
int n, m;
scanf("%d %d", &n, &m);
if (n % 2 == 0 && m % 2 == 0)
{
printf("%d", (n / 2) * (m / 2));
}
if (n % 2 == 0 && m % 2 == 1)
{
printf("%d", (n / 2) * ((m + 1) / 2));
}
if (n % 2 == 1 && m % 2 == 0)
{
printf("%d", ((n + 1) / 2) * (m / 2));
}
if (n % 2 == 1 && m % 2 == 1)
{
printf("%d", ((n + 1) / 2) * ((m + 1) / 2));
}
}
``````
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 &);
vector < string> split(const string &);

/*
* Complete the 'gameWithCells' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
*  1. INTEGER n
*  2. INTEGER m
*/

int gameWithCells(int n, int m) {

int x=(n+1)/2;
if(x==0)
x=1;
int y=(m+1)/2;
if(y==0)
y=1;
return (x*y);
}

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

string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);

vector < string> first_multiple_input = split(rtrim(first_multiple_input_temp));

int n = stoi(first_multiple_input[0]);

int m = stoi(first_multiple_input[1]);

int result = gameWithCells(n, m);

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

vector < string> split(const string &str) {
vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));

}
``````
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 'gameWithCells' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
*  1. INTEGER n
*  2. INTEGER m
*/

public static int gameWithCells(int n, int m)
{
int mOddConstant= m % 2;
int nOddConstant= n % 2;
int result = ((m+mOddConstant)/2)*((n+nOddConstant)/2);
return result;
}

}

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

int n = Convert.ToInt32(firstMultipleInput[0]);

int m = Convert.ToInt32(firstMultipleInput[1]);

int result = Result.gameWithCells(n, m);

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 'gameWithCells' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
*  1. INTEGER n
*  2. INTEGER m
*/

public static int gameWithCells(int n, int m) {
int counter = 0;
int ncount = 0;
if(m%2!=0)
{
counter = (m/2)+1;
}else{
counter = (m/2);
}
if (n>2 && n%2!=0)
{
ncount = (n/2) + 1;
counter = counter * ncount;
}else if(n>2 && n%2==0){
ncount = n/2;
counter = counter * ncount;
}
return counter;

}

}

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

int n = Integer.parseInt(firstMultipleInput[0]);

int m = Integer.parseInt(firstMultipleInput[1]);

int result = Result.gameWithCells(n, m);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

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 'gameWithCells' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
*  1. INTEGER n
*  2. INTEGER m
*/

function gameWithCells(n, m) {
if(m % 2 != 0){
m = Math.floor(m / 2) + 1;
}else if(m % 2 == 0){
m = m / 2;
}

if(n % 2 != 0){
n = Math.floor(n / 2) + 1;
}else if(n % 2 == 0){
n = n / 2;
}

return m*n;

}

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

const firstMultipleInput = readLine().replace(/\s+\$/g, '').split(' ');

const n = parseInt(firstMultipleInput[0], 10);

const m = parseInt(firstMultipleInput[1], 10);

const result = gameWithCells(n, m);

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 'gameWithCells' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER n
#  2. INTEGER m
#

def gameWithCells(n, m):
return (m//2+m%2)*(n//2+n%2)

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

first_multiple_input = input().rstrip().split()

n = int(first_multiple_input[0])

m = int(first_multiple_input[1])

result = gameWithCells(n, m)

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

fptr.close()
``````
Copy The Code &