Created
February 4, 2025 08:49
-
-
Save Nasah-Kuma/ede843e2f3335a737a6a1599ec0143b3 to your computer and use it in GitHub Desktop.
Hacker Rank Challenge: https://www.hackerrank.com/challenges/between-two-sets/problem?isFullScreen=true
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 getTotalX(a, b) { | |
// Write your code here | |
let lowerBoundInt = a[a.length - 1]; | |
let upperBoundInt = b[0]; | |
let arrayInt = []; | |
let arrayForFactorsOfA = []; | |
let arrayWhoseFactorsAreB = []; | |
let finalArr = []; | |
if(lowerBoundInt > upperBoundInt) { | |
return 0; | |
} | |
for(let i=lowerBoundInt; i<= upperBoundInt; i++) { | |
arrayInt.push(i); | |
} | |
arrayInt.forEach(item => { | |
let count = 0 | |
a.forEach((element, i) => { | |
(item % element === 0) && count++; | |
(count === a.length) && arrayForFactorsOfA.push(item); | |
}) | |
}); | |
arrayInt.forEach(item => { | |
let count = 0 | |
b.forEach((element, i) => { | |
(element % item === 0) && count++; | |
(count === b.length) && arrayWhoseFactorsAreB.push(item); | |
}) | |
}); | |
//finds the items that exist in both arrays | |
finalArr = arrayWhoseFactorsAreB.filter(item => arrayForFactorsOfA.includes(item)) | |
return finalArr.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment