Stuck at a wrong answer


#1

getting wrong answer i.e., -30.5 whereas it must be -20 can someone suggest what’s wrong

float MO2(int a,int b){
return (a+b)/2.0;
}

float MO3(int a,int b,int c){
return a+b+c-(max(a,max(b,c)))-(min(a,min(b,c)));
}

float MO4(int a,int b,int c,int d){
int MAX=max(a,max(b,max(c,d)));
int MIN=min(a,min(b,min(c,d)));
cout<<MAX<<MIN;
cout<<a+c;
return (a+b+c+d-MAX-MIN)/2.0;
}

double findMedian(const vector &A,int m,const vector &B,int n){
if(n==0){
if(m==0)return -1;
if(m%2==0)return (A[m/2]+A[m/2-1])/2.0;
else return A[m/2];
}

if(n==1){
    if(m==1)return MO2(A[0],B[0]);
    
    if(m & 1) return MO2(A[m/2],MO3(A[m/2-1],A[m/2+1],B[0]));
    
    return MO3(A[m/2-1],A[m/2],B[0]);
}

else if(n==2){
    cout<<"Q";
    if(m==2)MO4(A[0],A[1],B[0],B[1]);
    
    if(m & 1) {return MO3(max(A[m/2-1],B[0]),min(A[m/2+1],B[1]),A[m/2]);}
    
     return MO4(max(A[m/2-2],B[0]),min(A[m/2+1],B[1]),A[m/2],A[m/2-1]);
}

int idxA=(m-1)/2;
int idxB=(n-1)/2;

vector<int>::const_iterator first = A.begin() + idxA;
vector<int>::const_iterator last = A.begin() + (m-1);
vector<int> newA(first, last);

/*vector<int>::const_iterator first3 = A.begin() + 0;
vector<int>::const_iterator last3 = A.begin() + idxA;
vector<int> A2(first3, last3);*/

vector<int>::const_iterator first2 = B.begin() + idxB;
vector<int>::const_iterator last2 = B.begin() + (n-1);
vector<int> newB(first2, last2);

/*vector<int>::const_iterator first4 = B.begin() + 0;
vector<int>::const_iterator last4 = B.begin() + idxB;
vector<int> B2(first2, last2);*/

if(A[idxA]<=B[idxB]) {cout<<"IN";return findMedian(newA,m/2+1,B,n-idxB);}
else {cout<<"OUT";return findMedian(A,m/2+1,newB,n-idxB);}

}

double Solution::findMedianSortedArrays(const vector &A, const vector &B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

int m=A.size();
int n=B.size();

if(n<m)
return findMedian(A,m,B,n);
else return findMedian(B,n,A,m);

}