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 7657279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:59:24+00:00 2026-05-31T12:59:24+00:00

Here I have written code for finding median of two sorted arrays: #include<iostream> using

  • 0

Here I have written code for finding median of two sorted arrays:

#include<iostream>
using namespace std;
#define L  5
#define  M 6
 const int N=L+M;
int A[1000];//define 1 indexed aarray
int B[1000];
int max(int c,int d){
    return (c>=d)?c:d;

}
int min(int c,int d)
{
    return (c<=d)?c:d;
}

void  read(){
    cout<<" enter A array "<<endl;
    for (int i=1;i<=L;i++)
        cin>>A[i];
    cout<<endl;
    cout<<"enter B array  "<<endl;
    for (int i=1;i<=M;i++)
        cin>>B[i];
    cout<<endl;


}
int median(int a[],int b[],int left,int right){
    if (left>right) {
        return median(b,a,max(1,(N/2)-L),min(M,N/2));
    }
    int i=int(left+right)/2;
    int j=int(N/2)+i;
    if((j==0 || a[i]>b[j]) && (j==M || a[i]<=b[j+1])){
        return a[i];
    }
    else
    {
        if((j==0 || a[i]>b[j])  &&(j!=M && a[i]>b[j+1]))
        return median(a,b,left,i-1);
    }


        return median(a,b,i+1,right);

}

int main(){




    return 0;
}

My question is what could be left and right values? It is from introduction to algorithms, I just don’t understand what are values of left and right variables?
I have defined left and right as 1 and N and tested with following arrays:

3 5 7 9 11 13
1 2 4 8 10

Answer is 13, which is not correct sure, what is wrong?

  • 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-31T12:59:25+00:00Added an answer on May 31, 2026 at 12:59 pm

    The homework problem you cited in a comment has what looks to be a pretty good explanation of left and right, including the starting values for them:

    Let the default values for left and right be such that calling
    MEDIAN-SEARCH(A,B) is equivalent to

    MEDIAN-SEARCH(A[1 ..l],B[1 ..m],max(1,ceil(n/2) - m),min(l,ceil(n/2))) 
    

    The invariant in MEDIAN-SEARCH(A,B) is that the median is always in
    either A[left ..right] or B. This is true for the initial call because
    A and B are sorted, so by the definition of median it must be between
    max(1,ceil(n/2) - m) and min(l,ceil(n/2)), inclusive. It is also true
    the recursive calls on lines 8 and 9, since the algorithm only
    eliminates parts of the array that cannot be the median by the
    definition of median. The recursive call on line 2 also preserves the
    invariant since if left > right the median must be in B be­tween the
    new left and right values.

    If you work through the algorithm on paper with small arrays, it should become more clear what’s going on. The algorithm converges in only a few steps if your arrays are smaller than a total of say 16 elements, so it should be quite workable on paper.

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

Sidebar

Related Questions

Here's a snippet of code from a shell script I have written: for src
Here's the thing: I have two applications, written in C++ and running on two
Here's my question. Right now I have a Linux server application (written using C++
Here i have written a small code...in this looping is not done..only first image
Hi I work with netbeans. I have written a code which has two classes
I have written code for server and client sockets. I have used threads using
Here I have written some code to get the square of a number from
I have written this code and what I am trying to do here is
I have written code for passing file descriptors between unrelated processes using streams. The
Just wondering what little scripts/programs people here have written that helps one with his

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.