Solutions for Merging Two Arrays

    • @MatthewIdete
    • @alexander, submitted in Python 3, achived 100% score
    def nextGap(gap):
    
        if (gap <= 1):
            return 0
        return (gap // 2) + (gap % 2)
    
    
    def merge(arr1, arr2, n, m):
    
        gap = n + m
        gap = nextGap(gap)
        while gap > 0:
    
            # comparing elements in
            # the first array.
            i = 0
            while i + gap < n:
                if (arr1[i] > arr1[i + gap]):
                    arr1[i], arr1[i + gap] = arr1[i + gap], arr1[i]
    
                i += 1
    
            # comparing elements in both arrays.
            j = gap - n if gap > n else 0
            while i < n and j < m:
                if (arr1[i] > arr2[j]):
                    arr1[i], arr2[j] = arr2[j], arr1[i]
    
                i += 1
                j += 1
    
            if (j < m):
    
                # comparing elements in the
                # second array.
                j = 0
                while j + gap < m:
                    if (arr2[j] > arr2[j + gap]):
                        arr2[j], arr2[j + gap] = arr2[j + gap], arr2[j]
    
                    j += 1
    
            gap = nextGap(gap)
    
    def solve(array,arr):
        n= len(array)
        m=len(arr)
        merge(array,arr,n,m)
        return array, arr