Created
February 26, 2016 15:44
-
-
Save tongrhj/5d9ae80d8e34e09d208d to your computer and use it in GitHub Desktop.
Hackertrail Fixx Digital Code Challenge Feb 2016
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bake your code here | |
importPackage(java.io); | |
importPackage(java.lang); | |
var stdin = new BufferedReader(new InputStreamReader(System['in'])); | |
var numberOfPackets = parseInt(stdin.readLine(), 10); | |
while (numberOfPackets >= 1) { | |
var totalCandies = 0; | |
var candyPackets = []; | |
var numberOfMoves = 0; | |
for (var i = 0; i < numberOfPackets; i++) { | |
var candiesInPacket = parseInt(stdin.readLine(), 10); | |
candyPackets.push(candiesInPacket); | |
totalCandies += candiesInPacket; | |
} | |
if (totalCandies % numberOfPackets > 0) { | |
print(-1); | |
} else { | |
var averageCandies = totalCandies / numberOfPackets; | |
candyPackets.forEach(function(candiesInPacket) { | |
if (candiesInPacket > averageCandies) { numberOfMoves += candiesInPacket - averageCandies } | |
}) | |
print(numberOfMoves); | |
} | |
numberOfPackets = stdin.readLine(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment