# Fair Rations – HackerRank Solution

In this post, we will solve Fair Rations HackerRank Solution. This problem (Fair Rations) is a part of HackerRank Problem Solving series.

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:

1. 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Â orÂ i – 1).
2. 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].

## Constraints

• 2 <= N <= 1000
• 1 <= B[i] <= 10, where 1 <= i <= n

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:

1. GiveÂ 1Â loaf of bread each to the second and third people so that the distribution becomesÂ (2, 4, 5, 5, 6).
2. 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.

## Solution – Fair Rations – HackerRank Solution

### C++

```#include <iostream>
#include <vector>
#include <math.h>

using namespace std;

int main(){
int N;
cin >> N;

vector<int> B(N);

for(int B_i = 0;B_i < N;B_i++){
cin >> B[B_i];
}

int i = 0, odd = 0, p = 0, q = 0;

/* Logic behind this code is after all distribution according question all loaves will be even
* only if, number of person having odd number of loaves initially is even
* For example: n = 5
2 3 4 5 6
Here, only two person have odd number of loaves initially; Therefore, in
* in last even distribution is possible;
* Where as if, for example: n = 5
2 3 4 5 7
Here, we cannot distribute them according to question because initially
* 3 person have odd number of loaves */

while(i<N){
if(B[i] % 2 != 0){
odd++;          // finds total number of odds;
i++;
/* Whenever total number of odd (variable) is even means distribution is possible */
if(odd % 2 == 0){
q += ++p;
p = 0;
}

while((B[i] % 2 == 0) && (i<N) && (odd % 2 != 0)){
p++;
i++;
}
}
else
i++;
}

if(odd % 2 == 0)
cout << 2*q << endl;
else
cout << "NO" << endl;
return 0;
}
```

### Python

```import sys

def check(array):
return len(list(filter(lambda x: x%2 == 0, array))) == len(array)

def fairRations(B):
res = 0

for ind in range(len(B)-1):
if B[ind]%2 == 1:
B[ind] += 1
B[ind+1] += 1
res += 2

return res if check(B) else 'NO'

if __name__ == "__main__":
N = int(input().strip())
B = list(map(int, input().strip().split(' ')))
result = fairRations(B)
print(result)
```

### Java

```import java.util.*;
import java.math.*;
import java.io.*;

public class Solution {

static Scanner in = new Scanner(new BufferedInputStream(System.in));
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String args[]) {
int n = in.nextInt();
int lst = 0;
int ans = 0;
for(int i = 0; i < n; i ++) {
int now = in.nextInt();
now += lst;
if(now % 2 != 0) {
ans += 2;
lst = 1;
} else lst = 0;
}
if(lst == 1) out.println("NO");
else out.println(ans);
out.flush();
}

static class pii implements Comparable<pii> {
int X, Y;
public int compareTo(pii a) {
return this.X - a.X;
}
}
}
```

Note: This problem (Fair Rations) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.