import scala.collection.mutable._
object Main {
def miniVariationLength(nums: Array[Int], threshold: Int): Int = {
val maxDeque = new ArrayDeque[Int]
val minDeque = new ArrayDeque[Int]
var start = 0
var end = 0
var ans = 0
while (end < nums.length) {
while (!minDeque.isEmpty && (nums(end) < nums(minDeque(minDeque.size-1)))) {
minDeque.removeLast()
}
while ( !maxDeque.isEmpty && (nums(end) > nums(maxDeque(maxDeque.size-1)))) {
maxDeque.removeLast()
}
minDeque += end
maxDeque += end
val variation = nums(maxDeque.head) - nums(minDeque.head)
if (variation > threshold) {
start += 1
if (start > minDeque.head)
minDeque.removeHead()
if (start > maxDeque.head)
maxDeque.removeHead()
}
ans = Math.max(ans, end - start + 1)
end += 1
}
ans
}
def main(args: Array[String]): Unit = {
val trafficRates = Array(10, 1, 2, 4, 7, 2)
val thresholdMiniVal = 5
println("The traffic of this customer changes by less than or equal to " + thresholdMiniVal + " Gbps in a " + miniVariationLength(trafficRates, thresholdMiniVal) + " day window")
}
}