GeeksforGeeks » C/C++ Programming Questions

an optimum algorithm

(3 posts)

Tags:

No tags yet.

  1. gowtham
    guest
    Posted 1 year ago #

    pls suggest an 0(n) algorithm..

  2. gowtham
    guest
    Posted 1 year ago #

    (This is the problem):
    You are given two sorted arrays, A and B, and A has a large enough
    buffer at the end to
    hold B. Write a method to merge B into A in sorted order.

  3. ahbiv
    Member
    Posted 1 year ago #

    Use the concept of merge sort...

    start from the last element of A and B, find the max between those two elements and copy it to the A's last position.
    do this for all elements.

    eg:
    A={5,7,13,25,36,50} len=6

    B={3,6,10,35,55} len=5

    then do merging
    A[10]=max(50,55)=55, i=5, j=4
    A[9]=max(50,35)=50, i=5, j=3
    A[8]=max(36,35)=36, i=4, j=3 and so on

    finally A={3,5,6,7,10,13,25,35,36,50,55} len=11

    Time complexity O(m+n)


Reply

You must log in to post.

RSS feed for this topic