Last active
April 29, 2023 04:01
-
-
Save espinoco/3c70224b66eb993b66cae70caeda072f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function getAllDigits(arr) { | |
const digits = []; | |
for (const number of arr) { | |
if (number === 0) { | |
digits.push(0); | |
continue; | |
} | |
let remainingOfNumber = Math.abs(number); | |
while (remainingOfNumber > 0) { | |
const digit = remainingOfNumber % 10; | |
digits.push(digit); | |
remainingOfNumber = Math.floor(remainingOfNumber / 10); | |
} | |
} | |
return digits; | |
} | |
function getDigitsMap(digitsMap, digit) { | |
if (digitsMap.has(digit)) { | |
digitsMap.set(digit, digitsMap.get(digit) + 1); | |
} else { | |
digitsMap.set(digit, 1); | |
} | |
return digitsMap; | |
} | |
function getDigitsSum(digitsSum, digit) { | |
return digitsSum + digit; | |
} | |
function constructNumberFromDigits(digits) { | |
return digits.reduce((accumulator, digit) => (accumulator * 10) + digit, 0); | |
} | |
function canConstructDivisibleNumberFromDigitsPermutation(digits, divisor) { | |
if (digits.length === 1) { | |
return digits[0] % divisor === 0; | |
} | |
for (let i = 0; i < digits.length; i++) { | |
let j = 0; | |
let k = 1; | |
while (j < digits.length && k < digits.length) { | |
const temp = digits[k]; | |
digits[k] = digits[j]; | |
digits[j] = temp; | |
k++; | |
j++; | |
const permutation = constructNumberFromDigits(digits); | |
if (permutation % divisor === 0) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
function digitsMapHasOnlyOneDigitAndIsDivisible(digitsMap: Map<number, number>, divisor) { | |
if (digitsMap.size === 1) { | |
const [ firstMapEntryKey, firstMapEntryValue ] = digitsMap.entries().next().value; | |
return firstMapEntryValue === 1 && firstMapEntryKey % divisor === 0; | |
} | |
return false; | |
} | |
function isPossibleToConstructNumberDivisibleBy1(arr) { | |
return getAllDigits(arr).length > 0; | |
} | |
function isPossibleToConstructNumberDivisibleBy2(arr) { | |
const digitsMap = getAllDigits(arr).reduce(getDigitsMap, new Map()); | |
return digitsMap.has(0) || digitsMap.has(2) || digitsMap.has(4) || digitsMap.has(6) | |
|| digitsMap.has(8); | |
} | |
function isPossibleToConstructNumberDivisibleBy3(arr) { | |
return getAllDigits(arr).reduce(getDigitsSum) % 3 === 0; | |
} | |
function isPossibleToConstructNumberDivisibleBy4(arr) { | |
const digitsMap = getAllDigits(arr).reduce(getDigitsMap, new Map()); | |
if (digitsMap.has(1) && (digitsMap.has(2) || digitsMap.has(6))) { | |
return true; | |
} | |
if (digitsMap.has(2) && (digitsMap.get(0) || digitsMap.has(4) || digitsMap.has(8))) { | |
return true; | |
} | |
if (digitsMap.has(3) && (digitsMap.has(2) || digitsMap.has(6))) { | |
return true; | |
} | |
if (digitsMap.has(4) && (digitsMap.get(0) || digitsMap.get(4) > 1 || digitsMap.has(8))) { | |
return true; | |
} | |
if (digitsMap.has(5) && (digitsMap.has(2) || digitsMap.has(6))) { | |
return true; | |
} | |
if (digitsMap.has(6) && (digitsMap.get(0) || digitsMap.has(4) || digitsMap.has(8))) { | |
return true; | |
} | |
if (digitsMap.has(7) && (digitsMap.has(2) || digitsMap.has(6))) { | |
return true; | |
} | |
if (digitsMap.has(8) && (digitsMap.get(0) || digitsMap.has(4) || digitsMap.get(8) > 1)) { | |
return true; | |
} | |
if (digitsMap.has(9) && (digitsMap.has(2) || digitsMap.has(6))) { | |
return true; | |
} | |
return digitsMapHasOnlyOneDigitAndIsDivisible(digitsMap, 4); | |
} | |
function isPossibleToConstructNumberDivisibleBy5(arr) { | |
const digitsMap = getAllDigits(arr).reduce(getDigitsMap, new Map()); | |
return digitsMap.has(0) || digitsMap.has(5); | |
} | |
function isPossibleToConstructNumberDivisibleBy6(arr) { | |
return isPossibleToConstructNumberDivisibleBy2(arr) | |
&& isPossibleToConstructNumberDivisibleBy3(arr); | |
} | |
function isPossibleToConstructNumberDivisibleBy7(arr) { | |
return canConstructDivisibleNumberFromDigitsPermutation(getAllDigits(arr), 7); | |
} | |
function isPossibleToConstructNumberDivisibleBy8(arr) { | |
const digitsMap = getAllDigits(arr).reduce(getDigitsMap, new Map()); | |
if (digitsMap.has(1) && digitsMap.has(6)) { | |
return true; | |
} | |
if (digitsMap.has(2) && digitsMap.has(4)) { | |
return true; | |
} | |
if (digitsMap.has(3) && digitsMap.has(2)) { | |
return true; | |
} | |
if (digitsMap.has(4) && (digitsMap.has(0) || digitsMap.has(8))) { | |
return true; | |
} | |
if (digitsMap.has(5) && digitsMap.has(6)) { | |
return true; | |
} | |
if (digitsMap.has(6) && digitsMap.has(4)) { | |
return true; | |
} | |
if (digitsMap.has(7) && digitsMap.has(2)) { | |
return true; | |
} | |
if (digitsMap.has(8) && (digitsMap.has(0) || digitsMap.get(8) > 1)) { | |
return true; | |
} | |
if (digitsMap.has(9) && digitsMap.has(6)) { | |
return true; | |
} | |
return digitsMapHasOnlyOneDigitAndIsDivisible(digitsMap, 8); | |
} | |
function isPossibleToConstructNumberDivisibleBy9(arr) { | |
return getAllDigits(arr).reduce(getDigitsSum) % 9 === 0; | |
} | |
export default function divisibleIntegers(n: number, arr: number[]): boolean { | |
switch (n) { | |
case 1: | |
return isPossibleToConstructNumberDivisibleBy1(arr); | |
case 2: | |
return isPossibleToConstructNumberDivisibleBy2(arr); | |
case 3: | |
return isPossibleToConstructNumberDivisibleBy3(arr); | |
case 4: | |
return isPossibleToConstructNumberDivisibleBy4(arr); | |
case 5: | |
return isPossibleToConstructNumberDivisibleBy5(arr); | |
case 6: | |
return isPossibleToConstructNumberDivisibleBy6(arr); | |
case 7: | |
return isPossibleToConstructNumberDivisibleBy7(arr); | |
case 8: | |
return isPossibleToConstructNumberDivisibleBy8(arr); | |
case 9: | |
return isPossibleToConstructNumberDivisibleBy9(arr); | |
default: | |
throw `Illegal divisor number: ${n}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment