Problem - Given three sorted arrays find combinations which are closest to each other.
example -
i/p - 3,8,18 7,11,16 10,15,19
o/p - (8,7,10)
i/p - 2,2,6 11,15,15 8,8,18
o/p - (6,11,8)
Kindly review this scala code and suggest improvements
import scala.math.Ordering._
object NearestMatch {
def main(args: Array[String]) = {
val size = args(0).toInt
val range = args(1).toInt
val (a,b,c) = (Array.fill(size)(scala.util.Random.nextInt(range)).sorted,
Array.fill(size)(scala.util.Random.nextInt(range)).sorted,
Array.fill(size)(scala.util.Random.nextInt(range)).sorted)
println(a.mkString(","))
println(b.mkString(","))
println(c.mkString(","))
var (i,j,k) = (0,0,0)
var (pa,pb,pc) = (0,0,0)
var curDiff = 12345 // a very large number
while (i < a.size && j < b.size && k < c.size) {
val min = Math.min(a(i), Math.min(b(j), c(k)))
val max = Math.max(a(i), Math.max(b(j), c(k)))
val newDiff = max-min
if (curDiff > newDiff) {
pa = i
pb = j
pc = k
curDiff = newDiff
}
if (a(i) == min) i+=1
else if (b(j) == min) j+=1
else k+=1
}
println(a(pa), b(pb), c(pc))
}
}