# HackerRank Fair Rations Solution

#### ByBrokenprogrammers

Dec 12, 2022

Hello Programmers, In this post, you will know how to solve the HackerRank Fair Rations Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## HackerRank Fair Rations 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 castles 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

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:

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.

## HackerRank Fair Rations Solution

### Fair Rations Solution in C

```#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int N,i,c;
scanf("%d",&N);
int *B = malloc(sizeof(int) * N);
c=0;
for(int B_i = 0; B_i < N; B_i++){
scanf("%d",&B[B_i]);
if(B[B_i]%2==1)c++;
}
int l=0;
if(c%2==0){
for(i=0;i<N-1;i++){
if(B[i]%2==1){
l=l+2;
B[i]=B[i]+1;
B[i+1]=B[i+1]+1;
}
}
printf("%d",l);
}
else printf("NO");
return 0;
}```

### Fair Rations Solution in Cpp

``` #include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
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];
B[B_i] %= 2;
}

int ans = 0;
for (int i = 0; i < N - 1; ++i) {
if (B[i] == 1) {
ans += 2;
B[i]--;
B[i + 1] = (B[i + 1] + 1) % 2;
}
}

if (B[N - 1] == 1) cout << "NO" << endl;
else cout << ans << endl;

return 0;
}```

### Fair Rations Solution in Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int B[] = new int[N];
for(int B_i=0; B_i < N; B_i++){
B[B_i] = in.nextInt();
}
int count = 0;
for (int i = 0; i < N - 1; i++) {
if (B[i] % 2 != 0) {
B[i + 1]++;
count += 2;
}
}
if (B[N - 1] % 2 == 0) {
System.out.println(count);
}
else {
System.out.println("NO");
}
}
}```

### Fair Rations Solution in Python

```#!/bin/python
import sys
N = int(raw_input().strip())
B = map(int,raw_input().strip().split(' '))
ans = 0
for i in xrange(N-1):
if B[i]%2 == 1:
B[i] += 1
B[i+1] += 1
ans += 2

if B[N-1] % 2 == 1:
print "NO"
else:
print ans```

### Fair Rations Solution using JavaScript

```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();
});
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
B = B.map(Number);
var count = 0;
for(var i = 0; i < N - 1; i++) {
if (B[i] % 2 == 0) { continue; }

B[i] = B[i] + 1;
B[i + 1] = B[i + 1] + 1;

count += 2;
}
if (B.some(function (v) { return v % 2 == 1; }))
console.log('NO');
else
console.log(count);
}```

### Fair Rations Solution in Scala

```object Solution extends App {
val lines = io.Source.stdin.getLines()
val ss = lines.drop(1).next().split(" ").map(_.toInt)
println(min(ss, ss.length).getOrElse("NO"))
def min(ss: Array[Int], l: Int, current: Int = 0, count: Int = 0): Option[Int] = {
if(current == l-1) {
if(ss(current)%2==0) {
Option(count)
} else {
None
}
} else if(ss(current) % 2 == 1){
ss(current) += 1
ss(current+1) += 1
min(ss, l, current+1, count+2)
} else {
min(ss, l, current+1, count)
}
}
}```

### Fair Rations Solution in Pascal

```(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
uses math;
var  n,i:longint;
a:array[0..10000] of longint;
sum:longint;
begin
for i:=1 to n do
for i:=1 to n-1 do
if a[i] mod 2=1 then
begin
a[i]:=a[i]+1;
inc(sum,2);
inc(a[i+1]);
end;
//writeln(a
if (a[n] mod 2=1) and (a[n-1] mod 2=0) then
writeln('NO') else
if  a[n] mod 2=0 then
writeln(sum) else writeln(sum+2);
end.
```

Disclaimer: This problem (Fair Rations) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.