Last active
April 10, 2019 06:12
-
-
Save unilecs/4344db4052d4b71f30de9e932c2c54ec to your computer and use it in GitHub Desktop.
Contains dividers in the array of number
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
function containsDividers(intArray, X) { | |
// HashMap: число -> кол-во вхождений в массив | |
let hashMapElements = {}; | |
for(let item of intArray) { | |
if (hashMapElements[item]) { | |
++hashMapElements[item]; | |
} else { | |
hashMapElements[item] = 1; | |
} | |
} | |
for(let item in hashMapElements) { | |
item = parseInt(item); | |
// второй делитель X: | |
// X = item * div | |
let div = item != 0 ? X / item : X; | |
let remainder = item != 0 ? X % item : X; | |
// Если мы нашли число (item), ктр делит X без остатка | |
if (remainder === 0) { | |
// Кол-во вхождений второго делителя в исходный массив | |
let countOfDiv = hashMapElements[div]; | |
// X = div * div, в этом случае countOfDiv > 1 | |
// Или X = div * item, в этом случае countOfDiv > 0 | |
if (div == item && countOfDiv > 1 || | |
div != item && countOfDiv == 1) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
console.log(containsDividers([-1, 2, -1], 1)); // true | |
console.log(containsDividers([0, 0], 0)); // true | |
console.log(containsDividers([0, 3, 3, 10], 3)); // false | |
console.log(containsDividers([1, 0, 3, 3, 9, 15], 10)); // false | |
console.log(containsDividers([1, 0, 3, 3, 20, 8], 10)); // false | |
console.log(containsDividers([1, 0, -3, 3, -10], -9)); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment