HackerRank Fair Rations problem solution YASH PAL, 31 July 20247 December 2025 In this 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 , 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.Hackerrank Fair Rations problem solution in Python.#!/bin/python3 import sys def solve(b): if sum(b) % 2 == 1: return 'NO' tot = 0 for i in range(len(b) - 1): if b[i] % 2 == 1: b[i] = b[i] + 1 b[i+1] = b[i+1] + 1 tot = tot + 2 return tot N = int(input().strip()) B = [int(B_temp) for B_temp in input().strip().split(' ')] print(solve(B))Fair Rations problem solution in Java Programming.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; } } }Problem solution in C++ programming.#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; }Problem solution in C programming.#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, B_i, i, count = 0; scanf("%d",&N); int *B = malloc(sizeof(int) * N); for (B_i = 0; B_i < N; B_i++){ scanf("%d", &B[B_i]); } for (i = 0; i < N; i++) { if (B[i] % 2 == 1) { B[i]++; count++; if (!(i + 1 < N)) { printf("NOn"); exit(0); } else { B[i + 1]++; count++; } } } printf("%dn", count); return 0; }Problem solution in JavaScript programming.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() { var numSubjects, numLoaves, numLoavesDistributed, i; numSubjects = +readLine(); numLoaves = readLine().split(' ').map(Number); numLoavesDistributed = 0; for (i = 0; i < numSubjects - 1; i++) { if (numLoaves[i] % 2 === 1) { numLoaves[i]++; numLoaves[i + 1]++; numLoavesDistributed += 2; } } if (numLoaves[numSubjects - 1] % 2 === 0) { console.log(numLoavesDistributed); } else { console.log('NO'); } } Algorithms coding problems solutions AlgorithmsHackerRank