In this HackerRank Fair Rations problem you have 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


Problem solution in Python programming.

#!/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))


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("NO\n");
        exit(0);
      }
      else {
        B[i + 1]++;
        count++;
      }
    }
  }
  printf("%d\n", 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');
    }
}