-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.java
90 lines (60 loc) · 1.83 KB
/
Solution.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Problem: https://www.hackerrank.com/challenges/fair-rations
//Java 8
/*
Examples:
[1 1 1 2 1 2 1 2 1 1 2 2 1 2]
[2 2 1 2 1 2 1 2 1 1 2 2 1 2] 2
[2 2 2 3 1 2 1 2 1 1 2 2 1 2] 2
[2 2 2 4 2 2 1 2 1 1 2 2 1 2] 2
[2 2 2 4 2 2 2 3 1 1 2 2 1 2] 2
[2 2 2 4 2 2 2 4 2 1 2 2 1 2] 2
[2 2 2 4 2 2 2 4 2 2 3 2 1 2] 2
[2 2 2 4 2 2 2 4 2 2 4 3 1 2] 2
[2 2 2 4 2 2 2 4 2 2 4 4 2 2] 2
16
[2 3 4 5 6]
[2 4 5 5 6] 2
[2 4 6 6 6] 2
4
[1 1 1 1 1 1 1 1 1 1 1 1] solvable
[1 1 1 1 1 1 1 1 1 1 1 1 1] unsolvable
Time: O(n)
Space: O(1)
*/
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 bread = 0;
int currentBread = 0;
for(int i = 0; i < N; i++)
{
currentBread += in.nextInt();
if(i == N-1)//We have reached the last person
{
if(currentBread % 2 == 1) //If last person ended with odd bread it is not possible
{
System.out.println("NO");
System.exit(0);
}
else
{
System.out.println(bread);
System.exit(0);
}
}
if(currentBread % 2 == 1) //The current person has odd bread give them and the next person bread
{
currentBread = 1;
bread += 2;
continue;
}
currentBread = 0; // No extra bread was given out
}
}
}