0% found this document useful (0 votes)
4 views14 pages

8. ARRAY 05

The document discusses solutions for two problems from LeetCode: 'Two Sum' and 'Range Sum Query - 2D'. The 'Two Sum' problem involves finding pairs of numbers in an array that sum to a target, with solutions using naive, hashing, and two-pointer approaches, while the 'Range Sum Query - 2D' problem focuses on efficiently calculating the sum of elements in a specified rectangular region of a 2D matrix using a prefix sum matrix. Each solution includes complexity analysis and example cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

8. ARRAY 05

The document discusses solutions for two problems from LeetCode: 'Two Sum' and 'Range Sum Query - 2D'. The 'Two Sum' problem involves finding pairs of numbers in an array that sum to a target, with solutions using naive, hashing, and two-pointer approaches, while the 'Range Sum Query - 2D' problem focuses on efficiently calculating the sum of elements in a specified rectangular region of a 2D matrix using a prefix sum matrix. Each solution includes complexity analysis and example cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ARRAY PRACTICE - 05

04
16 February 2024 17:21

01 - TWO SUM ( LEETCODE - 01 )


02 - RANGE SUM QUERY - 2D
( LEETCODE - 304 )

CREATED BY - SHIVAM BAREKAR


TWO SUM ( LEETCODE - 01 )
19 February 2024 22:49

Problem Statement : Check if a pair with given sum exists in Array


Given an array of integers arr[] and an integer target.
1st variant : Return YES if there exist two numbers such that their sum is equal to
the target. Otherwise, return NO.
2nd variant : Return indices of the two numbers such that their sum is equal to the
target. Otherwise, we will return {-1, -1}.

Link : Two Sum - LeetCode

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 :

Input Format: N = 5, arr[] = {2,6,5,8,11}, target = 14


Result: YES (for 1st variant)
[1, 3] (for 2nd variant)
Explanation : arr[1] + arr[3] = 14. So, the answer is “YES” for the first variant and [1, 3]
for 2nd variant.

Example 2 :

Input Format: N = 5, arr[] = {2,6,5,8,11}, target = 15


Result: NO (for 1st variant)
[-1, -1] (for 2nd variant)
Explanation : There exist no such two numbers whose sum is equal to the target.

Solution-01 : Naive Approach(Brute-force approach) :

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 :

Given array, nums = [2,1,3,4], target = 4


Using the naive approach, we first select one number and then find the second one.
For index 0, element= 2,
Then, we iterate through indices 1 to 3 to check if target – x, i.e. 4 – 2 = 2 exists. 2 does
not exist from index 1 to 3, we move to the next index.
For index 1, element=1,
Then, we iterate through indices 2 to 3 to find if target – x, i.e. 4 – 1 = 3 exists. 3 exists
at index 2, so we store the indices 1 and 2, break the loop, and return the indices.

Code Varient-01 :
Output :

This is the answer for variant 1: YES

Complexity Analysis :

Time Complexity : O(N2), where N = size of the array.


Reason: There are two loops(i.e. nested) each running for approximately N times.
Space Complexity : O(1) as we are not using any extra space.

Code Varient-02 :

Output :

This is the answer for variant 2: [1, 3]

Complexity Analysis :

Time Complexity : O(N2), where N = size of the array.


Reason: There are two loops(i.e. nested) each running for approximately N times.
Space Complexity : O(1) as we are not using any extra space.

Solution-02 : Better Approach(using Hashing) :

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 :

The steps are as follows :


• We will select the element of the array one by one using a loop(say i).
• Then we will check if the other required element(i.e. target-arr[i]) exists in the hashMap.
• If that element exists, then we will return “YES” for the first variant or we will return
the current index i.e. i, and the index of the element found using map i.e. mp[target-
arr[i]].
• If that element does not exist, then we will just store the current element in the hashMap
along with its index. Because in the future, the current element might be a part of our
answer.
• Finally, if we are out of the loop, that means there is no such pair whose sum is equal to
the target. In this case, we will return either “NO” or {-1, -1} as per the variant of the
question.

Dry Run :

Given array, nums = [2,3,1,4], target = 4

Note : Here x denotes the currently selected element.

For index 0, x = 2, and currently map is empty.


We try to find if target – x = 4 – 2 = 2 is present in the map or not.
For now, 2 does not exist on the map.
And we store the index of element 2. i.e., mp[2] = 0,

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 :

This is the answer for variant 1: YES

Complexity Analysis :

Time Complexity : O(N), where N = size of the array.


Reason : The loop runs N times in the worst case and searching in a hashmap takes O(1)
generally. So the time complexity is O(N).
Note : In the worst case(which rarely happens), the unordered_map takes O(N) to find an
element. In that case, the time complexity will be O(N2). If we use map instead of
unordered_map, the time complexity will be O(N* logN) as the map data structure takes
logN time to find an element.
Space Complexity : O(N) as we use the map data structure.
Note : We have optimized this problem enough. But if in the interview, we are not allowed
to use the map data structure, then we should move on to the following approach i.e. two
pointer approach. This approach will have the same time complexity as the better
approach.

Code Varient-02 :

Output :

This is the answer for variant 2: [1, 3]

Complexity Analysis :

Time Complexity : O(N), where N = size of the array.


Reason: The loop runs N times in the worst case and searching in a hashmap takes O(1)
generally. So the time complexity is O(N).
Note: In the worst case(which rarely happens), the unordered_map takes O(N) to find an
element. In that case, the time complexity will be O(N2). If we use map instead of
unordered_map, the time complexity will be O(N* logN) as the map data structure takes
logN time to find an element.

Space Complexity : O(N) as we use the map data structure.


Note: We have optimized this problem enough. But if in the interview, we are not allowed
to use the map data structure, then we should move on to the following approach i.e. two
pointer approach. This approach will have the same time complexity as the better
approach.

Solution-03 : Optimized Approach(using two-pointer) :

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 :

The steps are the following :


• We will sort the given array first.
• Now, we will take two pointers i.e. left, which points to the first index, and right, which
points to the last index.
• Now using a loop we will check the sum of arr[left] and arr[right] until left < right.
○ If arr[left] + arr[right] > sum, we will decrement the right pointer.
○ If arr[left] + arr[right] < sum, we will increment the left pointer.
○ If arr[left] + arr[right] == sum, we will return the result.
• Finally, if no results are found we will return “No” or {-1, -1}.

Dry Run :

Given array, nums = [2,1,3,4], target = 4

• First, we sort the array. So nums after sorting are [1,2,3,4]


• We take two-pointers, left and right. The left points to index 0 and the right points to
index 3.
• Now we check if nums[left] + nums[right] == target. In this case, they don’t sum up, and
nums[left] + nums[right] > target so that we will reduce right by 1.
• Now, left = 0, right=2
• Here, nums[left] + nums[right] == 1 + 3 == 4, which is the required target, so we will return
the result.

Output :

This is the answer for variant 1: YES

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 :

Time Complexity : O(N) + O(N*logN), where N = size of the array.


Reason: The loop will run at most N times. And sorting the array will take N*logN time
complexity.
Space Complexity : O(1) as we are not using any extra space.
Note: Here we are distorting the given array. So, if we need to consider this change, the
space complexity will be O(N).
RANGE SUM QUERY – 2D ( LEETCODE - 304 )
19 February 2024 22:51

Problem Statement : Given a 2D matrix matrix, handle multiple queries of the


following type :
• Calculate the sum of the elements of matrix inside the rectangle defined by its upper
left corner (row1, col1) and lower right corner (row2, col2).

Implement the NumMatrix class :


• NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
• int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements
of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower
right corner (row2, col2).
You must design an algorithm where sumRegion works on O(1) time complexity.

Link : https://leetcode.com/problems/range-sum-query-2d-immutable/

Example-01 :
Input :

["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]


[[[[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]]], [2, 1, 4, 3], [1, 1,
2, 2], [1, 2, 2, 4]]

Output :

[null, 8, 11, 12]

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

-104 <= matrix[i][j] <= 104


0 <= row1 <= row2 < m
0 <= col1 <= col2 < n

At most 104 calls will be made to sumRegion.

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.

You might also like