-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFair Rations.cpp
More file actions
56 lines (46 loc) · 1.5 KB
/
Copy pathFair Rations.cpp
File metadata and controls
56 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Que is at :- HackerRank > HourRank9 > Fair Ration*/
#include <iostream>
#include <vector>
#include <math.h>
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];
}
int i = 0, odd = 0, p = 0, q = 0;
/* Logic behind this code is after all distribution according question all loaves will be even
* only if, number of person having odd number of loaves initially is even
* For example: n = 5
2 3 4 5 6
Here, only two person have odd number of loaves initially; Therefore, in
* in last even distribution is possible;
* Where as if, for example: n = 5
2 3 4 5 7
Here, we cannot distribute them according to question because initially
* 3 person have odd number of loaves */
while(i<N){
if(B[i] % 2 != 0){
odd++; // finds total number of odds;
i++;
/* Whenever total number of odd (variable) is even means distribution is possible */
if(odd % 2 == 0){
q += ++p;
p = 0;
}
while((B[i] % 2 == 0) && (i<N) && (odd % 2 != 0)){
p++;
i++;
}
}
else
i++;
}
if(odd % 2 == 0)
cout << 2*q << endl;
else
cout << "NO" << endl;
return 0;
}