[Leetcode solution] Median of Two Sorted Arrays in cpp

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

[[code]]czoxMjc3OlwiDQpkb3VibGUgZmluZE1lZGlhblNvcnRlZEFycmF5cyhpbnQgQVtdLCBpbnQgbSwgaW50IEJbXSwgaW50IG4pIHsNCml7WyYqJl19bnQgbWVkPTA7DQppZigobStuKSUyID09MSkgPHNwYW4gc3R5bGU9XCJjb2xvcjogIzMzOTk2NjtcIj4vL2lmIG9kZDwvc3Bhbj4gDQpte1smKiZdfWVkPShtK24pLzIgKyAxOyA8c3BhbiBzdHlsZT1cImNvbG9yOiAjMzM5OTY2O1wiPi8vdGFrZSBtaWRkbGUgdmFsdWU8L3NwYW4+DQplbHtbJiomXX1zZQ0KbWVkPShtK24pLzIgOyA8c3BhbiBzdHlsZT1cImNvbG9yOiAjMzM5OTY2O1wiPi8vZWxzZSB0YWtlIGF2ZyBvZiB0d28gbWlkIHZ7WyYqJl19YWx1ZTwvc3Bhbj4NCg0KaW50IGNvdW50PTA7DQppbnQgaT0wLCBqPTA7DQpmb3IoaSxqO2kmbHQ7bSAmYW1wOyZhbXA7IGombHQ7bntbJiomXX07KSA8c3BhbiBzdHlsZT1cImNvbG9yOiAjMzM5OTY2O1wiPi8vbG9vcCB0ZXJtaW5hdGVzIGlmIEEgb3IgQiBlbmQgICAgIC8vaXMgcmV7WyYqJl19YWNoZWQ8L3NwYW4+DQp7DQpjb3VudCsrOw0KaWYoQVtpXSAmbHQ7PSBCW2pdKQ0Kew0KDQppZiggY291bnQgPT0gbWVkICZhbXA7JntbJiomXX1hbXA7ICgobStuKSUyID09MSkpDQpyZXR1cm4gQVtpXTsNCmVsc2UgaWYoIGNvdW50ID09IG1lZCAmYW1wOyZhbXA7ICgobStuKSUye1smKiZdfSAhPTEpKQ0Kew0KaWYoaSAmbHQ7IG0gLTEgJmFtcDsmYW1wOyBBW2krMV0gJmx0Oz0gQltqXSkNCnsNCnJldHVybiAoZG91YmxlKSh7WyYqJl19QVtpXStBW2krMV0pLzIgOw0KfQ0KZWxzZQ0KcmV0dXJuIChkb3VibGUpKEFbaV0rQltqXSkvMjsNCn0NCmkrKzsNCn0NCmVsc2UNCntbJiomXX17DQppZihjb3VudCA9PSBtZWQgJmFtcDsmYW1wOyAoKG0rbiklMiA9PTEpICkNCnJldHVybiBCW2pdOw0KZWxzZSBpZiggY291bnQge1smKiZdfT09IG1lZCAmYW1wOyZhbXA7ICgobStuKSUyICE9MSkpDQp7DQppZihqICZsdDsgbiAtMSAmYW1wOyZhbXA7IEJbaisxXSAmbHQ7IEF7WyYqJl19W2ldKQ0KcmV0dXJuIChkb3VibGUpKEJbal0rQltqKzFdKS8yIDsNCmVsc2UNCnJldHVybiAoZG91YmxlKShCW2pdK0FbaV0pLzI7DXtbJiomXX0KfQ0KaisrOw0KfQ0KfQ0Kd2hpbGUoaSZsdDttKQ0Kew0KY291bnQrKzsNCmlmKGNvdW50ID09IG1lZCApDQp7DQppZigobStuKSUye1smKiZdfSA9PTEpDQpyZXR1cm4gQVtpXTsNCmVsc2UNCnJldHVybiAoZG91YmxlKShBW2ldK0FbaSsxXSkvMjsNCn0NCmkrKzsNCn0NCg0Kd2h7WyYqJl19aWxlKGombHQ7bikNCnsNCmNvdW50Kys7DQppZihjb3VudCA9PSBtZWQgKQ0Kew0KaWYoKG0rbiklMiA9PTEpDQpyZXR1cm4gQltqXXtbJiomXX07DQplbHNlDQpyZXR1cm4gKGRvdWJsZSkoQltqXStCW2orMV0pLzI7DQp9DQpqKys7DQp9DQpyZXR1cm4gLTE7DQp9DQpcIjt7WyYqJl19[[/code]]

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

Your email address will not be published. Required fields are marked *