- Practice
- Algorithms
- Implementation
- Fair Rations
- Discussions

# Fair Rations

# Fair Rations

WatchandLearn + 0 comments In this problem the thing is if sum of all number is even ,then array can be even. if sum of all number is odd , then the answer is NO.

My java solution is here;

public class Solution {

`public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int B[] = new int[N]; int sum = 0; for(int B_i=0; B_i < N; B_i++){ B[B_i] = in.nextInt(); sum+=B[B_i]; } int count = 0; if(sum % 2 == 1){ System.out.println("NO"); } else{ for(int i = 0; i<N; i++){ if(B[i] % 2 != 0){ B[i] = B[i] + 1; B[i+1] = B[i+1] + 1; count+=2; } } System.out.println(count); } }`

}

sataaa + 0 comments Hmmm.. I made a completely different algorithm than the proposed one on the Editorial.

First, I check if there is an odd number of odds, because the way I understood it, when you give an odd a loaf, you "push the odd forward"... So it needs to reach another odd number so that both become even.

When I realised that, all that was left, after checking there was an even number of odds (so every odd could be pushed into another one), was to get the distances between odd numbers and multiply them by 2, because on every "push" you give 2 loaves of bread.

It's a real simple algorithm and isn't a greedy one.

eng_sidan + 0 comments how can i return "NO" in C while the function return int type ?

klinMlin + 0 comments I don't know if anyone else will run into the same problem, but the solution I found found was change the code to

int result = fairRations(B); if(result==-1) bufferedWriter.write("NO"); else bufferedWriter.write(String.valueOf(result));

If the out put is NO, return -1.

[deleted] + 0 comments In this version (C++) we don't use a vector and we use the input on the fly. We also run only one for loop, the one required to get all the variables of the list. Please feedback if there is some way to improve the following code:

#include <string> #include <iostream> 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; }

Sort 439 Discussions, By:

Please Login in order to post a comment