Skip to content

Instantly share code, notes, and snippets.

@Shaddyjr
Created July 14, 2020 22:26
Show Gist options
  • Save Shaddyjr/299680250c3b332cf06937ea48ee36dc to your computer and use it in GitHub Desktop.
Save Shaddyjr/299680250c3b332cf06937ea48ee36dc to your computer and use it in GitHub Desktop.
// source: https://www.hackerrank.com/challenges/organizing-containers-of-balls
function organizingContainers(containers) {
const n = containers.length;
// keep # of balls per container
const containerCounts = new Array(n).fill(0);
// keep # of types of balls
const ballTypes = new Array(n).fill(0);
for(let i = 0; i < n; i++){
const container = containers[i];
for(let j = 0; j < n; j++){
const ballCount = container[j];
containerCounts[i] += ballCount;
ballTypes[j] += ballCount;
}
}
// sort to make comparable
containerCounts.sort();
ballTypes.sort();
for(let i = 0; i<n;i++){
if(containerCounts[i] !== ballTypes[i]) return "Impossible";
}
return "Possible";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment