Problem

  • Initially have a 1D array of 0
  • Operation is always addition
  • Input is range followed by no to add [a b k]
  • Challenge is being able to use correctdata_type for long values
  • LEARNING:

Solution

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
 
class Solution {
 
    // Complete the arrayManipulation function below.
      static long arrayManipulation(int noOfZerosInArr, int[][] queries, int m)
    {
 long[] matrix = new long[noOfZerosInArr+1]; // getting index error
        for (int i = 0; i < m; i++)
        {
            int[] entity = queries[i];
            int firstIndex = entity[0];
            int secondIndex = entity[1];
            int valToAdd = entity[2];
            matrix[firstIndex] += valToAdd;
            if ((secondIndex + 1) <= noOfZerosInArr) {
	            matrix[secondIndex + 1] -= valToAdd;
	          }
        }
 
        long tempMax = 0;
        long max = 0;
        for (int i = 1; i <= noOfZerosInArr; i++)
        {
            tempMax += matrix[i];
            if (tempMax > max) max = tempMax;
        }       
        
        return max;
    }
 
    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
 
        string[] nm = Console.ReadLine().Split(' ');
        int n = Convert.ToInt32(nm[0]);
        int m = Convert.ToInt32(nm[1]);
        int[][] queries = new int[m][];
 
        for (int i = 0; i < m; i++) {
            queries[i] = Array.ConvertAll(Console.ReadLine().Split(' '), queriesTemp => Convert.ToInt32(queriesTemp));
        }
 
        long result = arrayManipulation(n, queries,m);
        textWriter.WriteLine(result);
        textWriter.Flush();
        textWriter.Close();
    }
}