0% found this document useful (0 votes)
5 views15 pages

Array 02

The document outlines three array problems: merging two sorted arrays, calculating the running sum of a 1-D array, and finding the maximum of array elements except for a specified index. It provides detailed descriptions, examples, and solutions for each problem, including complexity analyses. Each problem is linked to its respective LeetCode submission for further reference.
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)
5 views15 pages

Array 02

The document outlines three array problems: merging two sorted arrays, calculating the running sum of a 1-D array, and finding the maximum of array elements except for a specified index. It provides detailed descriptions, examples, and solutions for each problem, including complexity analyses. Each problem is linked to its respective LeetCode submission for further reference.
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/ 15

ARRAY PROBLEMS - 02

13 February 2024 21:19

1. MERGE SORTED ARRAY


2. RUNNING SUM OF 1-D ARRAY
3. MAX OF ARRAY ELEMENT
EXCEPT ARR[i]

CREATED BY - SHIVAM BAREKAR


MERGE SORTED ARRAY
13 February 2024 20:45

Description :

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order,
and two integers m and n, representing the number of elements in nums1 and nums2
respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function, but instead be stored
inside the array nums1. To accommodate this, nums1 has a length of m + n, where
the first m elements denote the elements that should be merged, and the last n
elements are set to 0 and should be ignored. nums2 has a length of n.

Link : Merge Sorted Array - LeetCode

Example 1 :
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.

Example 2 :
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].

Example 3 :
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the
merge result can fit in nums1.

Constraints :

nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200

-109 <= nums1[i], nums2[j] <= 109

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

This approach is not the exact solution according to the question as in this approach we
are going to use an extra space i.e. an array. But it is definitely one of the solutions if the
question does not contain the constraint of not using any extra space. And also this
approach will help to understand the optimal approaches.

Approach :

Assume the size of the given arrays are n and m.


The steps are as follows :
• We will first declare a third array, arr3[] of size n+m, and two pointers i.e. left and
right, one pointing to the first index of arr1[] and the other pointing to the first
index of arr2[].
• The two pointers will move like the following :
○ If arr1[left] < arr2[right]: We will insert the element arr1[left] into the array
and increase the left pointer by 1.
○ If arr2[right] < arr1[left]: We will insert the element arr2[right] into the array
and increase the right pointer by 1.
○ If arr1[left] == arr2[right]: Insert any of the elements and increase that
particular pointer by 1.
○ If one of the pointers reaches the end, then we will only move the other pointer
and insert the rest of the elements of that particular array into the third array
i.e. arr3[].
• If we move the pointer like the above, we will get the third array in the sorted
order.
• Now, from sorted array arr3[], we will copy first n(size of arr1[]) elements to arr1[],
and the next m(size of arr2[]) elements to arr2[].

Intuition :

Intuition is pretty straightforward. As the given arrays are sorted, we are using 2 pointer
approach to get a third array, that contains all the elements from the given two arrays in
the sorted order. Now, from the sorted third array, we are again filling back the given two
arrays.
Dry Run :

The following dry run will further simplify the concept:


Assume arr1[] = [1 4 8 10] and arr2[] = [2 3 9].
Output :

The merged arrays are: arr1[] = 1 2 3 4 arr2[] = 8 9 10

Complexity Analysis :

Time Complexity: O(n+m) + O(n+m), where n and m are the sizes of the given arrays.
Reason: O(n+m) is for copying the elements from arr1[] and arr2[] to arr3[]. And another
O(n+m) is for filling back the two given arrays from arr3[].

Space Complexity: O(n+m) as we use an extra array of size n+m.

Solution-02 : Optimal Approach 1 (without using any extra space) :

In this optimal approach, we need to get rid of the extra space we were using.

Approach :

The sizes of the given arrays are n(size of arr1[]) and m(size of arr2[]).

The steps are as follows:


• We will declare two pointers i.e. left and right. The left pointer will point to the last index
of the arr1[](i.e. Basically the maximum element of the array). The right pointer will point
to the first index of the arr2[](i.e. Basically the minimum element of the array).
• Now, the left pointer will move toward index 0 and the right pointer will move towards the
index m-1. While moving the two pointers we will face 2 different cases like the following
:
○ If arr1[left] > arr2[right]: In this case, we will swap the elements and move the
pointers to the next positions.
○ If arr1[left] <= arr2[right]: In this case, we will stop moving the pointers as arr1[]
and arr2[] are containing correct elements.
• Thus, after step 2, arr1[] will contain all smaller elements and arr2[] will contain all bigger
elements. Finally, we will sort the two arrays.

Intuition :

If we merge the given array, one thing we can assure is that arr1[] will contain all the
smaller elements and arr2[] will contain all the bigger elements. This is the logic we will
use. Using the 2 pointers, we will swap the bigger elements of arr1[] with the smaller
elements of arr2[] until the minimum of arr2[] becomes greater or equal to the maximum
of arr1[].
Dry run :
The dry run will further clarify the concepts :
Output :

The merged arrays are: arr1[] = 1 2 3 4 arr2[] = 8 9 10

Complexity Analysis :

Time Complexity: O(min(n, m)) + O(n*logn) + O(m*logm), where n and m are the sizes of the
given arrays.
Reason: O(min(n, m)) is for swapping the array elements. And O(n*logn) and O(m*logm) are
for sorting the two arrays.

Space Complexity: O(1) as we are not using any extra space.

Solution-03 : Optimal Approach 2 (Using gap method) :

This gap method is based on a sorting technique called shell sort. The intuition of this
method is simple.

Intuition :

Similar to optimal approach 1, in this approach, we will use two pointers i.e. left and right,
and swap the elements if the element at the left pointer is greater than the element at
the right pointer.

But the placing of the pointers will be based on the gap value calculated. The formula to
calculate the initial gap is the following :

Initial gap = ceil((size of arr1[] + size of arr2[]) / 2)

Assume the two arrays as a single continuous array and initially, we will place the left
pointer at the first index and the right pointer at the (left+gap) index of that continuous
array.

Now, we will compare the elements at the left and right pointers and move them by 1 place
each time after comparison. While comparing we will swap the elements if the element at
the left pointer > the element at the right pointer. After some steps, the right pointer will
reach the end and the iteration will be stopped.
After each iteration, we will decrease the gap and will follow the same procedure until the
iteration for gap = 1 gets completed. Now, after each iteration, the gap will be the
following :

gap = ceil( previous gap / 2)

The whole process will be applied to the imaginary continuous array constructed using
arr1[] and arr2[].

Approach :

The steps are as follows:

• First, assume the two arrays as a single array and calculate the gap value i.e. ceil((size of
arr1[] + size of arr2[]) / 2).

• We will perform the following operations for each gap until the value of the gap becomes 0
:
○ Place two pointers in their correct position like the left pointer at index 0 and the
right pointer at index (left+gap).

○ Again we will run a loop until the right pointer reaches the end i.e. (n+m). Inside the
loop, there will be 3 different cases:

▪ If the left pointer is inside arr1[] and the right pointer is in arr2[]: We will
compare arr1[left] and arr2[right-n] and swap them if arr1[left] > arr2[right-n].

▪ If both the pointers are in arr2[]: We will compare arr1[left-n] and arr2[right-
n] and swap them if arr1[left-n] > arr2[right-n].

▪ If both the pointers are in arr1[]: We will compare arr1[left] and arr2[right]
and swap them if arr1[left] > arr2[right].

○ After the right pointer reaches the end, we will decrease the value of the gap and it
will become ceil(current gap / 2).

• Finally, after performing all the operations, we will get the merged sorted array.
Output :

The merged arrays are: arr1[] = 1 2 3 4 arr2[] = 8 9 10

Complexity Analysis :

Time Complexity: O((n+m)*log(n+m)), where n and m are the sizes of the given arrays.
Reason: The gap is ranging from n+m to 1 and every time the gap gets divided by 2. So, the
time complexity of the outer loop will be O(log(n+m)). Now, for each value of the gap, the
inner loop can at most run for (n+m) times. So, the time complexity of the inner loop will be
O(n+m). So, the overall time complexity will be O((n+m)*log(n+m)).

Space Complexity: O(1) as we are not using any extra space.

Submission of solution on leetcode : https://leetcode.com/problems/merge-sorted-


array/submissions/1173890913
RUNNING SUM OF 1-D ARRAY
13 February 2024 20:46

Description :

Given an array nums. We define a running sum of an array as runningSum[i] =


sum(nums[0]…nums[i]).
Return the running sum of nums.

Link : Running Sum of 1d Array - LeetCode

Example 1:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

Example 2:
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].

Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]

Constraints :

1 <= nums.length <= 1000


-10^6 <= nums[i] <= 10^6

Solution : Prefix Sum

We directly traverse the array. For the current element nums[i], we add it with the prefix
sum nums[i−1] to get the prefix sum nums[i] of the current element.
Complexity Analysis :

Time complexity is O(n), where n is the length of the array.

Space complexity is O(1).

Submission link of Solution on leetcode : https://leetcode.com/problems/running-sum-


of-1d-array/submissions/1173907400
MAX OF ARRAY ELEMENT EXCEPT ARR[i]
13 February 2024 20:47

Description :

Given an array and M queries,each query will have an index I, we need to find max of
all array elements except arr[i].

Example :
Input – arr = {2,1,3,5,4,5,7,6}
q=4
4763
Output – 7 7 6 7

Approach : Prefix max and Suffix max

Steps :
• First we have to find the prefix max array and suffix max array.
• Then iterate over the queries array and check if element id is 0 then return suffix
max array of index query + 1. Else if element id is n-1 then return maximum(pre[id-
1],suf[id+1]).

Complexity Analysis :

Time Complexity : O(N+M)

You might also like