Q). Find Median using class and STL in C++
=> Solution :
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
double mediumResult;
int vectSize;
nums1.insert(nums1.end(), nums2.begin(), nums2.end());
sort(nums1.begin(),nums1.end());
vectSize=nums1.size();
if(vectSize%2==0){
mediumResult=double((nums1[vectSize/2-1]+nums1[vectSize/2]))/2;
}else{
mediumResult=(nums1[vectSize/2]);
}
return mediumResult;
}
};
int main(){
vector<int> vect1 = {1,2};
vector<int> vect2 = {3,4};
double medium;
Solution sol;
medium=sol.findMedianSortedArrays(vect1,vect2);
cout<<"Medium : "<<medium<<endl;
}
Comments
Post a Comment