We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all 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];