-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotos.java
More file actions
92 lines (89 loc) · 1.65 KB
/
Photos.java
File metadata and controls
92 lines (89 loc) · 1.65 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
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
91
92
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Photos {
public static void main(String[] args) throws IOException
{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(f.readLine());
int even = 0;
int odd = 0;
StringTokenizer input = new StringTokenizer(f.readLine());
for(int i = 0; i<n; i++)
{
if(Integer.valueOf(input.nextToken()) % 2 == 0)
{
even += 1;
}
else
{
odd +=1;
}
}
// main function
int res = 0;
if(odd == even)
{
res = odd *2;
}
else if(even > odd)
{
res = odd*2 + 1;
}
else
{
res = even * 2 + calc(odd-even);
}
PrintWriter out = new PrintWriter(new BufferedWriter (new OutputStreamWriter(System.out)));
System.out.println(res);
}
public static int calc(int num)
{
int count = 0;
int numLeft = num;
String status = "even"; // the next requirement of cows
if(num == 1)
{
count = -1;
}
else
{
//loop will always break before it ends, num is just a random limit
for(int i =0; i<num; i++)
{
if(numLeft == 0) // no more cows left
{
break;
}
else
{
if(numLeft == 4 && status.equals("even"))
{
count += 1;
break;
}
else
{
if(status.equals("even"))
{
numLeft -= 2;
status = "odd";
count += 1;
}
else
{
numLeft -= 1;
status = "even";
count += 1;
}
}
}
}
}
return count;
}
}