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

  • SEARCH
  • Home
  • 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 6916957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:40:10+00:00 2026-05-27T09:40:10+00:00

Given 2 arrays with int, A1 and A2. A1 length is L1, A2 length

  • 0

Given 2 arrays with int, A1 and A2. A1 length is L1, A2 length is L1+L2.
The first L2 elements of A1 and A2 have been sorted. Combine the first L2 elements into A2.
e.g.

   A1:  1  3  6  0
   A2:  2  5  7  13  10 22 11 
   result: 
   A1 + A2:  1 2 3 5 6 7 13 10 22  11

my solutions:

Pick each element by putting min { A1[i], A2[i]} into a array B[i], O(2 * L2)
Copy B to A2. O(L1 + L2).

Unsorted part should not be changed.

Any better solutions ?

thanks

  • 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-27T09:40:10+00:00Added an answer on May 27, 2026 at 9:40 am
    int* semiMerge(int*, int, int*, int);
    
    int main() {
      const int A1[] = {1, 3, 6, 0};
      const int A2[] = {2, 5, 7, 13, 10, 22, 11};
    
      const int L1 = sizeof(A1)/sizeof(int);
      const int L2 = sizeof(A2)/sizeof(int) - L1;
    
      int* out = semiMerge(A1, L1, A2, L2);
    }
    
    int* semiMerge(A1, L1, A2, L2) {
      int* output = new int[L2 + L2 + L1];
    
      //merge does a sorted combination of the items--both sets must be sorted up to the endpoints;
      //we want to merge only the first L1 results from each array
      std::merge(A1, A1 + L2,
                 A2, A2 + L2,
                 output);
      //at this point, we have an array of 2*L1 elements, all sorted properly.
    
    
      //we want to start looking at the first element we didn't copy from A2,
      //the -1 is to account for the fact that begin() + L1 is the start of the L1+1th slot
      std::copy(A2 + L2,
                A2 + L2 + L1, 
                (output + L2 + L2 - 1));
    
    return output;
    }
    

    I chose to show A1 and A1 as static arrays, but if you’re getting them as int*s to heap-allocated arrays, and if it’s important that the finished array be placed in L2, you can then say delete[] L2; L2 = out; after the call to semiMerge(). I chose not to do this in main because I represented A2 as a static array, while switching it out for the contents of out would require it to be a pointer to a dynamic array.

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

Sidebar

Related Questions

Given two sorted arrays: A and B . The size of array A is
For School I have been given a class interface to define my own array
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
Given a list of arrays and lots of setup time I need to quickly
Duplicate In C arrays why is this true? a[5] == 5[a] Given an array
I have a string. I need to replace all instances of a given array
Given an array of items, each of which has a value and cost ,
i have seen a few days ago such problem there is given two array
First of all, I don't have multiplication, division operations so i could use shifting/adding,
Scenario I have the following methods: public void AddItemSecurity(int itemId, int[] userIds) public int[]

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.