##### Fair Rations Hackerrank

You are the benevolent ruler of Rankhacker Castle, and today you’re distributing bread to a straight line of N subjects. Each i^{th} subject (where 1<=i<=N) already has B_{i} loaves of bread.

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 , 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).

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.

Input Format

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

The second line contains N space-separated integers describing the respective loaves of bread already possessed by each person in the line (i.e., B_{1}, B_{2},…. ,B_{N}).

Constraints

2<=N<1000
1<=B_{i}<=10, where 1<=i<=N
Output Format
Print a single integer denoting the minimum number of loaves you must distribute to adjacent people in the line so that every person has an even number of loaves; if it's not possible to do this, print NO.
Sample Input 0
5
2 3 4 5 6
Sample Output 0
4
Sample Input 1
2
1 2
Sample Output 1
NO
Explanation
Sample Case 0:
The initial distribution is (2, 3, 4, 5, 6). You can satisfy the problem's requirements by performing the following actions:
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, 3, 6, 6, 6).
Because each of the N subjects now has an even number of loaves, we can stop distributing bread. We then print the total number of loaves distributed, which is 4, on a new line.
Sample Case 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. Thus, we print NO as our answer.

##### Solution

#include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main(){ int N, input, oddity = 0, count = 0; cin >> N; for(int j = 0; j < N; j++){ cin >> input; if(input % 2) { count++; oddity = count % 2? oddity - j : oddity + j; } } cout << (count % 2 ? "NO" : to_string(oddity * 2)); return 0; }

```
n = input()
a = map(int, raw_input().strip().split())
cost = 0
for i in xrange(n):
if a[i] % 2:
if i == n-1:
print 'NO'
break
else:
a[i] += 1
a[i+1] += 1
cost += 2
else:
print cost
```

```
N = input()
B = map(int,raw_input().split(' '))
cnt = 0
for i in range(N-1):
if (B[i] % 2 == 1):
B[i] += 1
B[i+1] += 1
cnt += 2
if B[-1] % 2 == 1:
print "NO"
else:
print cnt
```

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

```
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");
}
}
}
```