Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- consider i pointing to the last element of arr1(actual elment, not zero), j point to the last elment of the arr2 and k point the actual of the arr1 end
- 3 Cases:
- 1.If i>=0 and j>=0, it will be populated with the bigger element
- 2.If all elements of the arr1 got over means i<0 then second array element will be directly populated.
- 3.If all elements of the arr2 got over means j<0, then the elments would already be in sorted array of arr1 so you can return
- '''
- class Solution:
- def merge(self, arr1: List[int], m: int, arr2: List[int], n: int) -> None:
- k=m+n-1
- i=m-1
- j=n-1
- print(k,i,j)
- while k>=0 and j>=0:
- if i>=0:
- if arr1[i]>=arr2[j]:
- arr1[k]=arr1[i]
- i-=1
- k-=1
- else:
- arr1[k]=arr2[j]
- j-=1
- k-=1
- else:
- arr1[k]=arr2[j]
- j-=1
- k-=1
Advertisement