0

could anyone explain what below if statement does

if (dict.contains(sum-k)) count+= dict(sum-k)
            dict(sum) += 1

Actual code:

object Solution {
    def subarraySum(nums: Array[Int], k: Int): Int = {
        if (nums isEmpty) return 0
    
    val dict = scala.collection.mutable.Map[Int, Int]().withDefaultValue(0)
    var count = 0
    
    nums.scanLeft(0)(_+_).foreach { sum => 
        **if (dict.contains(sum-k)) count+= dict(sum-k)
        dict(sum) += 1**
    }
    
    count
}

}

SCouto
  • 7,808
  • 5
  • 32
  • 49
Divi
  • 1
  • 2

0 Answers0