Array 02
Array 02
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.
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
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 :
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 :
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[].
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[]).
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 :
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.
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 :
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 :
The whole process will be applied to the imaginary continuous array constructed using
arr1[] and arr2[].
Approach :
• 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 :
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)).
Description :
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 :
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 :
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
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 :