`{"id":"8fb0164f-ea57-4e56-8faa-5fa7ddc71142","name":"Median Of Two Sorted Arrays","description":"Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.","inputFormat":"Input is managed for you","outputFormat":" Output is managed for you","constraints":"nums1.length == m\r\nnums2.length == n\r\n0 &lt;= m &lt;= 1000\r\n0 &lt;= n &lt;= 1000\r\n1 &lt;= m + n &lt;= 2000\r\n-10^6 &lt;= nums1[i], nums2[i] &lt;= 10^6","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nfloat find(vector<float> &a1,vector<float> &a2) {\n //write your code here\n \n}\n\nint main(){\n int n1, n2;\n //input work\n cin>>n1;\n vector<int> a(n1);\n\n for(int i=0; i<n1; i++){\n cin>>a[i];\n }\n vector<float>a1(n1);\n for(int i=0;i<n1;i++){\n a1[i]=(float)(a[i]);\n }\n\n cin>>n2;\n vector<int> b(n2);\n\n for(int i=0; i<n2; i++){\n cin>>b[i];\n }\n vector<float>b2(n2);\n for(int i=0;i<n2;i++){\n b2[i]=(float)(b[i]);\n }\n float median = find(a1,b2);\n int ab=median*10;\n string ans=to_string(ab);\n int k=ans.length();\n if(ans[k-1]=='0') cout<<median<<\".0\";\n else cout<<median;\n \n return 0;\n}"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n public static double find(int[]a1,int[]a2) {\r\n //write your code here\r\n return 0;\r\n }\r\n\r\n public static void main(String[]args) {\r\n Scanner scn = new Scanner(System.in);\r\n\r\n //input work\r\n int n1 = scn.nextInt();\r\n int[]a = new int[n1];\r\n\r\n for(int i=0;i < n1;i++){\r\n a[i] = scn.nextInt();\r\n }\r\n\r\n int n2 = scn.nextInt();\r\n int[]b = new int[n2];\r\n\r\n for(int i=0; i < n2;i++) {\r\n b[i] = scn.nextInt();\r\n }\r\n\r\n double median = find(a,b);\r\n System.out.println(median);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"5\r\n3 5 7 9 12\r\n3\r\n4 6 14","sampleOutput":"6.5","questionVideo":"https://www.youtube.com/embed/jDJuW7tSxio","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"16f57139-7b67-4feb-a33e-d33a7b31238b","name":"Median Of Two Sorted Arrays","slug":"median-of-two-sorted-arrays","type":1}],"next":{"id":"c8d073ae-00f1-4468-9626-a6846bc25dc8","name":"Median Of Two Sorted Arrays","type":3,"slug":"median-of-two-sorted-arrays"},"prev":{"id":"0464d5c8-ea6b-49e3-888f-8aadb588b25c","name":"Find The Smallest Divisor Given A Threshold MCQ","type":0,"slug":"find-the-smallest-divisor-given-a-threshold-mcq-2pmdt"}}}`

# Median Of Two Sorted Arrays

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

`{"id":"8fb0164f-ea57-4e56-8faa-5fa7ddc71142","name":"Median Of Two Sorted Arrays","description":"Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.","inputFormat":"Input is managed for you","outputFormat":" Output is managed for you","constraints":"nums1.length == m\r\nnums2.length == n\r\n0 &lt;= m &lt;= 1000\r\n0 &lt;= n &lt;= 1000\r\n1 &lt;= m + n &lt;= 2000\r\n-10^6 &lt;= nums1[i], nums2[i] &lt;= 10^6","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nfloat find(vector<float> &a1,vector<float> &a2) {\n //write your code here\n \n}\n\nint main(){\n int n1, n2;\n //input work\n cin>>n1;\n vector<int> a(n1);\n\n for(int i=0; i<n1; i++){\n cin>>a[i];\n }\n vector<float>a1(n1);\n for(int i=0;i<n1;i++){\n a1[i]=(float)(a[i]);\n }\n\n cin>>n2;\n vector<int> b(n2);\n\n for(int i=0; i<n2; i++){\n cin>>b[i];\n }\n vector<float>b2(n2);\n for(int i=0;i<n2;i++){\n b2[i]=(float)(b[i]);\n }\n float median = find(a1,b2);\n int ab=median*10;\n string ans=to_string(ab);\n int k=ans.length();\n if(ans[k-1]=='0') cout<<median<<\".0\";\n else cout<<median;\n \n return 0;\n}"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n public static double find(int[]a1,int[]a2) {\r\n //write your code here\r\n return 0;\r\n }\r\n\r\n public static void main(String[]args) {\r\n Scanner scn = new Scanner(System.in);\r\n\r\n //input work\r\n int n1 = scn.nextInt();\r\n int[]a = new int[n1];\r\n\r\n for(int i=0;i < n1;i++){\r\n a[i] = scn.nextInt();\r\n }\r\n\r\n int n2 = scn.nextInt();\r\n int[]b = new int[n2];\r\n\r\n for(int i=0; i < n2;i++) {\r\n b[i] = scn.nextInt();\r\n }\r\n\r\n double median = find(a,b);\r\n System.out.println(median);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"5\r\n3 5 7 9 12\r\n3\r\n4 6 14","sampleOutput":"6.5","questionVideo":"https://www.youtube.com/embed/jDJuW7tSxio","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"16f57139-7b67-4feb-a33e-d33a7b31238b","name":"Median Of Two Sorted Arrays","slug":"median-of-two-sorted-arrays","type":1}],"next":{"id":"c8d073ae-00f1-4468-9626-a6846bc25dc8","name":"Median Of Two Sorted Arrays","type":3,"slug":"median-of-two-sorted-arrays"},"prev":{"id":"0464d5c8-ea6b-49e3-888f-8aadb588b25c","name":"Find The Smallest Divisor Given A Threshold MCQ","type":0,"slug":"find-the-smallest-divisor-given-a-threshold-mcq-2pmdt"}}}`

Editor

# Median Of Two Sorted Arrays

hard

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

## Constraints

nums1.length == m nums2.length == n 0 <= m <= 1000 0 <= n <= 1000 1 <= m + n <= 2000 -10^6 <= nums1[i], nums2[i] <= 10^6

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 3 5 7 9 12 3 4 6 14```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}6.5`

Question Video

Discussions

Show Discussion

Related Resources