8. ARRAY 05
8. ARRAY 05
04
16 February 2024 17:21
Note : You are not allowed to use the same element twice. Example : If the target is equal
to 6 and num[1] = 3, then nums[1] + nums[1] = target is not a solution.
Example 1 :
Example 2 :
Intuition :
For each element of the given array, we will try to search for another element such that
its sum is equal to the target. If such two numbers exist, we will return the indices or
“YES” accordingly.
Approach :
• First, we will use a loop(say i) to select the indices of the array one by one.
• For every index i, we will traverse through the remaining array using another loop(say
j) to find the other number such that the sum is equal to the target (i.e. arr[i] +
arr[j] = target).
Observation :
In every iteration, if the inner loop starts from index 0, we will be checking the same pair
of numbers multiple times. For example, in iteration 1, for i = 0, we will check for the pair
arr[0] and arr[1]. Again in iteration 2, for i = 1, we will check arr[1] and arr[0]. So, to
eliminate these same pairs, we will start the inner loop from i+1.
Dry Run :
Code Varient-01 :
Output :
Complexity Analysis :
Code Varient-02 :
Output :
Complexity Analysis :
Intuition :
Basically, in the previous approach we selected one element and then searched for the
other one using a loop. Here instead of using a loop, we will use the HashMap to check if
the other element i.e. target-(selected element) exists. Thus we can trim down the time
complexity of the problem.
And for the second variant, we will store the element along will its index in the HashMap.
Thus we can easily retrieve the index of the other element i.e. target-(selected element)
without iterating the array.
Approach :
Dry Run :
For index 1, x = 3
We try to find if target – x = 4 – 3 = 1 is present in the map or not.
For now, 1 does not exist on the map.
And we store the index of element 3. i.e., mp[3] = 1,
For index 2, x = 1
We try to find if target – i = 4 – 1 = 3 is present in the map or not. 3 exists in the map, so
we store index 2 and the value stored for key 3 in the map and break the loop. And return
[1,2].
Code Varient-01 :
Output :
Complexity Analysis :
Code Varient-02 :
Output :
Complexity Analysis :
Intuition :
In this approach, we will first sort the array and will try to choose the numbers in a
greedy way.
We will keep a left pointer at the first index and a right pointer at the last index. Now
until left < right, we will check the sum of arr[left] and arr[right]. Now if the sum < target,
we need bigger numbers and so we will increment the left pointer. But if sum > target, we
need to consider lesser numbers and so we will decrement the right pointer.
If sum == target we will return either “YES” or the indices as per the question.
But if the left crosses the right pointer, we will return “NO” or {-1, -1}.
Approach :
Dry Run :
Output :
Note : For variant 2, we can store the elements of the array along with its index in a new
array. Then the rest of the code will be similar. And while returning, we need to return the
stored indices instead of returning “YES”. But for this variant, the recommended approach
is approach 2 i.e. hashing approach.
Complexity Analysis :
Link : https://leetcode.com/problems/range-sum-query-2d-immutable/
Example-01 :
Input :
Output :
Explanation :
NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1,
0, 1, 7], [1, 0, 3, 0, 5]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
Constraints :
m == matrix.length
n == matrix[i].length
1 <= m, n <= 200
Intuition :
The given solution is for the problem of querying the sum of elements in a rectangular
region of a matrix. The intuition behind the solution is to precompute the prefix sum
matrix, where the value at each cell represents the sum of all elements in the submatrix
from (0,0) to that cell. Then for each query, the sum of the required region can be
calculated using the precomputed values in constant time.
Approach :
The approach used is to compute the prefix sum matrix in the constructor of
the NumMatrix class. For this, two nested loops are used to iterate over all cells of the
matrix. At each cell, the sum of the current row and all previous rows up to the current
row, and the sum of the current column and all previous columns up to the current column,
is calculated using the precomputed value from the previous row or column. Finally, the sum
of the current cell is calculated by subtracting the value of the top-left submatrix, which
was added twice.
For each query of a rectangular region, the sum of the submatrix is calculated using the
precomputed values of the prefix sum matrix. Depending on the location of the top-left
and bottom-right corners of the submatrix, different cases are handled to calculate the
final result using the precomputed values.
Complexity Analysis :
Time complexity :
O(mn), where m and n are the dimensions of the matrix, as it requires iterating over all
cells of the matrix. The time complexity of each query is O(1), as it only requires
calculating the sum of four cells using the precomputed values.
Space complexity :
O(mn), as the prefix sum matrix requires the same amount of space as the original matrix.