You are viewing a single comment's thread. Return to all comments →
Java
public static String fairRations(List<Integer> B) { int loaves = 0, evenCount = 0; for (int i = 0; i < B.size() - 1; i++) { if (B.get(i) % 2 != 0) { B.set(i, B.get(i) + 1); B.set(i + 1, B.get(i + 1) + 1); loaves += 2; } evenCount += (B.get(i) % 2 == 0) ? 1 : 0; } evenCount += (B.get(B.size() - 1) % 2 == 0) ? 1 : 0; return evenCount != B.size() ? "NO" : String.valueOf(loaves); }
Seems like cookies are disabled on this browser, please enable them to open this website
Fair Rations
You are viewing a single comment's thread. Return to all comments →
Java