## Similar Problems

Similar Problems not available

# Patching Array - Leetcode Solution

LeetCode: Patching Array Leetcode Solution

Difficulty: Hard

Problem:

Given an array, arr, of size n and multiples queries, each query has three values: idx, val and inc. You must increase the array value at position idx by val and also increase all subsequent elements in the array by inc. You need to return the final array after all queries have been applied.

Example:

Input: arr = [1,2,3,4,5], queries = [[1,5,3],[4,10,2],[2,3,1]] Output: [1,8,11,14,18] Explanation: Initially, the array is [1,2,3,4,5]. Query 1: Increase the value at index 1 by 5. The array becomes [1,7,3,4,5]. Query 2: Increase the value at index 4 by 10 and all subsequent elements by 2. The array becomes [1,7,3,4,15]. Query 3: Increase the value at index 2 by 3 and all subsequent elements by 1. The array becomes [1,7,4,5,16]. The final array is [1,8,11,14,18].

Solution:

Approach: The question seems intimidating at first as we have a lot of queries and we are supposed to update the subsequent elements in the array too. However, if we look at the queries carefully, we can find a pattern that would enable us to come up with a solution. We can iterate through all the queries in order and for each query, we can update the idx value by the given val. After that, we increment all subsequent values in the array by the given increment using a simple for loop.

Algorithm:

- Initialize a variable 'n' to store the length of the array.
- Initialize a variable 'index' to keep track of the current index being processed.
- Iterate through all the queries in order, for each query:
- Update the idx value by the given val.
- Increment all subsequent values in the array by the given increment using a simple for loop.
- Return the final array after all queries have been applied.

Implementation:

Let's implement the above algorithm in Python.

def patchingArray(n,queries): arr = [0]*n # initialize the array with n zeros index = 0 # keep track of the current index being processed for query in queries: idx = query[0] val = query[1] inc = query[2]

```
# update the value at idx by val
arr[idx] += val
# increment all subsequent values by inc
for i in range(idx+1,n):
arr[i] += inc
return arr
```

The time complexity of the above algorithm is O(n*q) where n is the length of the array and q is the number of queries. However, this approach may not be the optimal solution for larger arrays or a large number of queries.

Optimized Solution:

Approach: The above solution has a time complexity of O(n*q), which may become impractical for larger arrays or a large number of queries. We can optimize this algorithm by calculating the final values for each index in the array using a dynamic programming approach. We will iterate through the queries in order and for each query, we will update the final array according to the given values. We can use a dictionary to store the previous index that was updated. Whenever we encounter a new index, we can calculate the intermediate values and update the array accordingly.

Algorithm:

- Initialize a variable 'n' to store the length of the array.
- Initialize a variable 'index' to keep track of the current index being processed.
- Initialize an empty dictionary 'prev'.
- Initialize the final array with n zeros.
- Iterate through all the queries in order, for each query:
- Find the previous index that was updated from the dictionary. If there is no such index, assume the previous index was -1.
- For the current query, update the intermediate value by adding val to all elements in the final array from the previous index+1 to the current index-1.
- Update the final array at the current index by adding the intermediate value to the val.
- If the current index is not in the dictionary, add it with the previous index value as the key and current index as the value.
- Return the final array.

Implementation:

Let's implement the above algorithm in Python.

def patchingArray(n,queries): prev = {} # store the previous index that was updated ans = [0]*n # initialize the final array with n zeros

```
for q in queries:
idx = q[0]
val = q[1]
inc = q[2]
# find the previous index that was updated
if idx-1 in prev:
start = prev[idx-1]+1
else:
start = 0
# update the intermediate value by adding val to all elements in the final array from start to idx-1
for i in range(start,idx):
ans[i] += val
# update the final array at the current index by adding the intermediate value to the val
ans[idx] += val
# store the current index as the previous index for future queries
prev[idx] = idx
# update all subsequent elements by the given increment
if idx+1<n:
ans[idx+1] += inc
return ans
```

The time complexity of the above algorithm is O(n+q) where n is the length of the array and q is the number of queries. Thus, this is a much more efficient solution than the previous one.

Conclusion:

In this question, we learned how to patch an array by updating the given elements and all subsequent elements in the array. We implemented two solutions: a simple brute-force approach and an optimized dynamic programming approach. The dynamic programming approach was much more efficient as it had a lower time complexity.

## Patching Array Solution Code

`1`