• + 0 comments

    public static long arrayManipulation(int n, List> queries) { // Write your code here // Use an array to store changes at boundaries long[] arr = new long[n + 2];

        // Process each query using the difference array technique
        for (List<Integer> query : queries) {
            int start = query.get(0);
            int end = query.get(1);
            int value = query.get(2);
    
            arr[start] += value;  // Add value at start index
            arr[end + 1] -= value; // Subtract value after the end index
        }
    
        // Find the maximum value by applying prefix sum
        long maxValue = 0;
        long currentSum = 0;
    
        for (int i = 1; i <= n; i++) {
            currentSum += arr[i];
            maxValue = Math.max(maxValue, currentSum);
        }
    
        return maxValue;
    }