• + 0 comments

    bit ugly but it does the job in scala (60)

    trick is to avoid inner loops for the queries and instead ADD value at the start index and SUBTRACT it at the end index + 1 (due to inclusive bounds)

    also make sure to return long

        def arrayManipulation(n: Int, queries: Array[Array[Int]]): Long = {
          val arr = Array.fill(n)(0)
    
          for (query <- queries) {
            val start = query(0) - 1
            val _end = query(1)
            val value = query(2)
    
            
            arr(start) = arr(start) + value
            if (_end != n) {
              arr(_end) = arr(_end) - value
            }
          }
          var res = 0L
          var run_res = 0L
          for (i <- 0 until n by 1) {
            run_res = run_res + arr(i)
            if (run_res > res){
              res = run_res
            }
          }
    					
    			
          res
        }