# HackerRank Fair Rations Problem Solution

In this post, we will solve HackerRank Fair Rations Problem Solution.

You are the benevolent ruler of Rankhacker Castle, and today you’re distributing bread. Your subjects are in a line, and some of them already have some loaves. Times are hard and your castle’s food stocks are dwindling, so you must distribute as few loaves as possible according to the following rules:

- Every time you give a loaf of bread to some person i, you must also give a loaf of bread to the person immediately in front of or behind them in the line (i.e., persons i + 1 ori – 1).
- After all the bread is distributed, each person must have an even number of loaves.

Given the number of loaves already held by each citizen, find and print the minimum number of loaves you must distribute to satisfy the two rules above. If this is not possible, print NO.**Example**

B = [4, 5, 6, 7]

- We can first give a loaf to i = 3 and i = 4 so B = [4, 5, 7, 8].
- Next we give a loaf to i = 2 and i = 3 and have B = [4, 6, 8, 8] which satisfies our conditions.

All of the counts are now even numbers. We had to distribute 4 loaves.

**Function Description**

Complete the *fairRations* function in the editor below.

fairRations has the following parameter(s):

*int B[N]:*the numbers of loaves each persons starts with

**Returns**

*string:*the minimum number of loaves required, cast as a string, or ‘NO’

**Input Format**

The first line contains an integer N, the number of subjects in the bread line.

The second line contains N space-separated integers B[i].

**Output Format**

**Sample Input 0**

STDIN Function ----- -------- 5 B[] size N = 5 2 3 4 5 6 B = [2, 3, 4, 5, 6]

**Sample Output 0**

`4`

**Explanation 0**

The initial distribution is (2, 3, 4, 5, 6). The requirements can be met as follows:

- Give 1 loaf of bread each to the second and third people so that the distribution becomes

(2, 4, 5, 5, 6). - Give 1 loaf of bread each to the third and fourth people so that the distribution becomes (2, 4, 6, 6, 6).

Each of the N subjects has an even number of loaves after 4 loaves were distributed.

**Sample Input 1**

```
2
1 2
```

**Sample Output 1**

```
NO
```

**Explanation 1**

The initial distribution is (1, 2). As there are only 2 people in the line, any time you give one person a loaf you must always give the other person a loaf. Because the first person has an odd number of loaves and the second person has an even number of loaves, no amount of distributed loaves will ever result in both subjects having an even number of loaves.

## Fair Rations C Solution

```
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,b[1005],i,count=0,flag=0;
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
if(b[i]%2==1 && i<n-1)
{
b[i+1]++;
count+=2;
}
// printf("%d\n",b[i]);
if(i==n-1 && b[i]%2==1)
flag=1;
}
if (flag==1)
printf("NO\n");
else
printf("%d\n",count);
return 0;
}
```

## Fair Rations C++ Solution

```
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int ans=0;
for(int i=0;i<n-1;i++){
if(arr[i]%2!=0){
arr[i]++;
arr[i+1]++;
ans+=2;
}
}
if(arr[n-1]%2==1){
printf("NO\n");
}else{
printf("%d\n",ans);
}
return 0;
}
```

## Fair Rations C Sharp Solution

```
//https://www.hackerrank.com/challenges/fair-rations
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int N = Convert.ToInt32(Console.ReadLine());
string[] B_temp = Console.ReadLine().Split(' ');
int[] B = Array.ConvertAll(B_temp, Int32.Parse);
int count = 0;
for (int i = 0; i < N - 1; i++)
{
if (B[i] % 2 > 0) // has odd number of loaves
{
B[i]++;
count++;
B[i + 1]++;
count++;
}
}
if (B[N-1] % 2 > 0)
{
Console.WriteLine("NO");
}
else
{
Console.WriteLine(count);
}
}
}
```

## Fair Rations Java Solution

```
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 'fairRations' function below.
*
* The function is expected to return a STRING.
* The function accepts INTEGER_ARRAY B as parameter.
*/
public static String fairRations(List<Integer> B) {
int distributions = 0;
for(int i=0;i<B.size()-1;i++){
if(B.get(i)%2!=0){
if(B.size()<3){
return "NO";
}
B.set(i+1,B.get(i+1)+1);
B.set(i,B.get(i)+1);
distributions+=2;
}
}
if(B.get(B.size()-1)%2 != 0){
return "NO";
}
return Integer.toString(distributions);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int N = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> B = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
String result = Result.fairRations(B);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
```

## Fair Rations JavaScript Solution

```
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
let N = parseInt(readLine());
let B = readLine().split(' ').map(Number);
let ret = 0
for (let i = 0; i < B.length-1; i++)
if (B[i] & 1) {
B[i+1]++
ret += 2
}
console.log(B[B.length-1] & 1 ? "NO" : ret)
}
```

## Fair Rations Python Solution

```
#!/bin/python3
import sys
N = int(input().strip())
B = [int(B_temp) for B_temp in input().strip().split(' ')]
c=0
if sum(B)%2:
print('NO')
else:
for i in range(N-1):
if B[i]%2:
B[i]+=1
B[i+1]+=1
c+=2
print(c)
```

other Solutions