Skip to content

Instantly share code, notes, and snippets.

@tongrhj
Created February 26, 2016 15:44
Show Gist options
  • Save tongrhj/5d9ae80d8e34e09d208d to your computer and use it in GitHub Desktop.
Save tongrhj/5d9ae80d8e34e09d208d to your computer and use it in GitHub Desktop.
Hackertrail Fixx Digital Code Challenge Feb 2016
// 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