[HackerRank] Array Manipulation

Array Manipulation

Problem

link: https://www.hackerrank.com/challenges/crush/problem

Solution

// Complete the arrayManipulation function below.
long arrayManipulation(int n, vector<vector<int>> queries) {
    vector<long> addArr(n,0);

    long max = 0;
    for (size_t i = 0; i<queries.size(); i++) {
        int a = queries[i][0]-1;
        int b = queries[i][1]-1;
        int k = queries[i][2];
        addArr[a] +=k;
        if ((b+1) <= n-1) addArr[b+1] -=k;
    }
    long tempMax = 0;
    for (size_t i = 0; i < n; i++){
        tempMax =tempMax+addArr[i];
        if (max <tempMax) max = tempMax;
    }
    return max;

}

Leave a Reply

Your email address will not be published. Required fields are marked *