Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1011357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:13:17+00:00 2026-05-16T09:13:17+00:00

My first array M + N size and second array of size N. let

  • 0

My first array M + N size and second array of size N.

let us say m=4,n=5
a[ ]= 1,3,5,7,0,0,0,0,0

b[ ]= 2,4,6,8,10

Now , how can i merge these two arrays without using external sorting algorithms and any other temporary array(inplace merge) but complexity should be o(n).Resultant array must be in sorted order.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:13:17+00:00Added an answer on May 16, 2026 at 9:13 am

    Provided a is exactly the right size and arrays are already sorted (as seems to be the case), the following pseudo-code should help:

    #    0 1 2 3 4 5 6 7 8
    a = [1,3,5,7,0,0,0,0,0]
    b = [2,4,6,8,10]
    afrom = 3
    bfrom = 4
    
    ato = 8
    while bfrom >= 0:
        if afrom == -1:
            a[ato] = b[bfrom]
            ato = ato - 1
            bfrom = bfrom - 1
        else:
            if b[bfrom] > a[afrom]:
                a[ato] = b[bfrom]
                ato = ato - 1
                bfrom = bfrom - 1
            else:
                a[ato] = a[afrom]
                ato = ato - 1
                afrom = afrom - 1
    print a
    

    It’s basically a merge of the two lists into one, starting at the ends. Once bfrom hits -1, there are no more elements in b so the remainder in a were less than the lowest in b. Therefore the rest of a can remain unchanged.

    If a runs out first, then it’s a matter of transferring the rest of b since all the a elements have been transferred above ato already.

    This is O(n) as requested and would result in something like:

    [1, 2, 3, 4, 5, 6, 7, 8, 10]
    

    Understanding that pseudo-code and translating it to your specific language is a job for you, now that you’ve declared it homework 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can declare a 2nd or multidimensional array without first size in c++? class
Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
/*Program to merge to arrays using pointers in descending order, when the first array
example some array {2,8,9,10,21,32,1,6,3...} first child take (data size / 2) and sort second
i have two arrays like this first array Array ( [0228] => Array (
Let's say I have the array 1,2,3,4,5,6,7,8,9,10,11,12 if my chunck size = 4 then
I've two PHP Arrays. The first one contains a sort order. The second one
The first array is called $related_docs and the second on is $all_docs. I'm trying
I'm using PostgreSQL 9.1.13 Here is what I've done so far: /* First Array
In my project ` $cid = $this->Surgical->find('first',array('conditions' => array('to_char(dt_surgery , \'DD-MM-YYYY\' )' =>'to_char(now() ,\'DD-MM-YYYY\')')));

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.