Created
January 27, 2017 10:27
-
-
Save vijayparashar12/56c6212cd53fbd8ad9d02a5a207d559b to your computer and use it in GitHub Desktop.
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
package hackerrank | |
/** | |
* Created by vparashar on 26/01/2017. | |
*/ | |
object Solution { | |
def main(args: Array[String]): Unit = { | |
val a = Seq(2, 4) | |
val b = Seq(16, 32, 96) | |
println(countBetween(a, b)) | |
} | |
def countBetween(a: Seq[Int], b: Seq[Int]) = { | |
val factors = b.map(i => factor(i)) | |
val commonFactors = factors.reduceLeft((i, j) => i.intersect(j)).toSet | |
val factorOfCommonFactors = commonFactors.map(factor).toList | |
val booleanList = factorOfCommonFactors.map(l => a.foldLeft(true)((bool, ele) => bool && l.contains(ele))) | |
val size = booleanList.foldLeft(0)((s, bool) => if (bool) s + 1 else s + 0) | |
size | |
} | |
private def factor(number: Int): Seq[Int] = { | |
val list = (1 to number).toList | |
list.filter(i => number % i == 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment