• + 2 comments

    Is there a reason all the solutions posted above are written inside main() and not the provided function arrayManipulation() ? Or did hackerrank just change this over the past few years for readability?

    // Complete the arrayManipulation function below.
    static long arrayManipulation(int n, int[][] queries) {
    
    // initialize array with 0's of size n
    long arr[] = new long[n];
    
    // each successive element contains the difference between itself and previous element
    
    for (int i = 0; i < queries.length; i++) {
    // when checking query, subtract 1 from both a and b since 0 indexed array
    int a = queries[i][0] - 1;
    int b = queries[i][1] - 1;
    int k = queries[i][2];
    
    arr[a] += k;
    if (b+1 < n) {
    arr[b+1] -= k;  
    }
    }
    
    // track highest val seen so far as we go
    long max = Long.MIN_VALUE;
    for (int i = 1; i < arr.length; i++) {
    arr[i] += arr[i-1];
    max = Math.max(arr[i], max);
    }
    
    return max;
    }