• + 0 comments

    Same solution in scala

      def arrayManipulation(n: Int, queries: Array[Array[Int]]): Long = {
        val arr = Array.fill[Int](n)(0)
    
        queries foreach { case Array(a, b, k) =>
          arr(a - 1) = arr(a - 1) + k
          if (b < n) arr(b) = arr(b) - k
        }
    
        val max = 0L
        val sum = 0L
    
        val (result, _) = arr.foldLeft((max, sum)) { case ((currentMax, currentSum), elem) =>
          val newSum = currentSum + elem
          if (newSum > currentMax) (newSum, newSum) else (currentMax, newSum)
        }
    
        result
      }