Merge two arrays by satisfying given constraints

def merge(X, Y, m, n):
 
    # size of `X` is `k+1`
    k = m + n + 1
 
    # run if `X` or `Y` has elements left
    while m >= 0 and n >= 0:
        # put the next greater element in the next free position in `X[]` from the end
        if X[m] > Y[n]:
            X[k] = X[m]
            m = m - 1
        else:
            X[k] = Y[n]
            n = n - 1
            k = k - 1
 
    # copy the remaining elements of `Y[]` (if any) to `X[]`
    while n >= 0:
        X[k] = Y[n]
        k = k - 1
        n = n - 1
 
    # fill `Y` with all zeroes
    for i in range(len(Y)):
        Y[i] = 0
 
 
# The function moves non-empty elements in `X` in the
# beginning and then merge them with `Y`
def rearrange(X, Y):
 
    # moves non-empty elements of `X` at the beginning
    k = 0
    for i in range(len(X)):
        if X[i]:
            X[k] = X[i]
            k = k + 1
 
    # merge `X[0… k-1]` and `Y[0… n-1]` into `X[0… m-1]`
    merge(X, Y, k - 1, len(Y) - 1)

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source