Skip to content

Instantly share code, notes, and snippets.

@danicunhac
Created January 5, 2025 18:01
Show Gist options
  • Save danicunhac/cc61666bec54da12a44d169135fc9b32 to your computer and use it in GitHub Desktop.
Save danicunhac/cc61666bec54da12a44d169135fc9b32 to your computer and use it in GitHub Desktop.
Higher Number Game (algorithms)
const game = (arr, k) => {
const rec = {};
let winner;
while (true) {
const st = arr[0];
const nd = arr[1];
if (st >= nd) {
arr.push(nd);
arr.splice(1, 1);
rec[st] ? rec[st]++ : (rec[st] = 1);
if (rec[st] === k) {
winner = st;
break;
}
} else {
arr.push(st);
arr.splice(0, 1);
rec[nd] ? rec[nd]++ : (rec[nd] = 1);
if (rec[nd] === k) {
winner = nd;
break;
}
}
continue;
}
return winner;
};
console.log(game([2, 3, 5, 7, 1, 4], 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment