Fair Rations HackerRank Solution in C, C++, Java, Python

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

For example, the people in line have loaves 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. We had to distribute 4 loaves.

Function Description

Complete the fairRations function in the editor below. It should return an integer that represents the minimum number of loaves required.

fairRations has the following parameter(s):

  • B: an array of integers that represent the number of loaves each persons starts with .

Input Format

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

The second line contains space-separated integers .

Output Format

Print a single integer taht denotes the minimum number of loaves that must be distributed 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 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.

Fair Rations HackerRank 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 HackerRank Solution in C++

#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 HackerRank 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 HackerRank 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 HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        int N = Convert.ToInt32(Console.ReadLine());
        string[] B_temp = Console.ReadLine().Split(' ');
        int[] B = Array.ConvertAll(B_temp,Int32.Parse);
        
        int loaves = 0;
        for (int i = 0; i < B.Length - 1; ++i) {
            if (B[i] % 2 == 1) {
                loaves += 2;
                B[i]++;
                B[i+1]++;
            }
        }
        
        if (B[B.Length - 1] % 2 == 1) {
            Console.Out.WriteLine("NO");
        } else {
            Console.Out.WriteLine(loaves);
        }
    }
}

 

Attempt Fair Rations HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/fair-rations/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/cavity-map-hackerrank-solution/

Leave a Comment