Advertisement
Iam_Sandeep

merge two arrays without extra space

Jul 31st, 2022
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1.  
  2. class Solution:
  3.     def merge(self,a,b,m,n):
  4.         nex=lambda x:x//2+(x%2) if x>1 else 0 #This is very important
  5.         gap=nex(m+n)
  6.         def swap(x,y,a,b):
  7.             a[x],b[y]=b[y],a[x]
  8.  
  9.         while gap:
  10.             i,j=0,gap
  11.             while j<m+n:
  12.                 if i<m and j<m:
  13.                     if a[i]>a[j]:swap(i,j,a,a)
  14.                 elif i<m and j>=m:
  15.                     if a[i]>b[j-m]:swap(i,j-m,a,b)
  16.                 elif i>=m and j>=m:
  17.                     if b[i-m]>b[j-m]:swap(i-m,j-m,b,b)
  18.                 i,j=i+1,j+1
  19.             gap=nex(gap)
  20.  
  21.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement