{"id":"b5a60c48-b108-42ba-a6d6-b1917d228ea5","name":"Merge Two Sorted Arrays","description":"1. You are given two sorted arrays(a,b) of integers.\r\n2. You have to merge them and form one sorted array.\r\n3. You have to do it in linear time complexity.","inputFormat":"An Integer n \r\na1\r\na2..n integers\r\nAn integer m\r\nb1\r\nb2..m integers","outputFormat":"Check the sample output and question video.","constraints":"1 &lt;= N &lt;= 10^8\r\n-10^9 &lt;= a[i],b[i] &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nvector<int> mergeTwoSortedArrays(vector<int> &A, vector<int> &B)\r\n{\r\n \r\n}\r\n\r\nvoid input(vector<int> &arr)\r\n{\r\n for (int i = 0; i < arr.size(); i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n}\r\n\r\nvoid output(vector<int> &arr)\r\n{\r\n for (int i = 0; i < arr.size(); i++)\r\n {\r\n cout << arr[i] << endl;\r\n }\r\n}\r\n\r\nint main()\r\n{\r\n int n, m;\r\n cin >> n;\r\n vector<int> A(n, 0);\r\n input(A);\r\n\r\n cin >> m;\r\n vector<int> B(m, 0);\r\n input(B);\r\n\r\n vector<int> ans = mergeTwoSortedArrays(A, B);\r\n output(ans);\r\n return 0;\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int[] mergeTwoSortedArrays(int[] a, int[] b){\r\n //write your code here\r\n \r\n return null;\r\n }\r\n\r\n public static void print(int[] arr){\r\n for(int i = 0 ; i < arr.length; i++){\r\n System.out.println(arr[i]);\r\n }\r\n }\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] a = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n a[i] = scn.nextInt();\r\n }\r\n int m = scn.nextInt();\r\n int[] b = new int[m];\r\n for(int i = 0 ; i < m; i++){\r\n b[i] = scn.nextInt();\r\n }\r\n int[] mergedArray = mergeTwoSortedArrays(a,b);\r\n print(mergedArray);\r\n }\r\n\r\n}"},"python":{"code":"def mergeArrays(arr1, arr2, n1, n2):\n\t# write your code\n \n\n\narr1= []\n\nn = int(input())\n\nfor i in range(0, n):\n\tele = int(input())\n\t\n\tarr1.append(ele)\n\t\narr2 =[]\nm = int(input())\n\nfor i in range(0, m):\n\tele = int(input())\n\n\tarr2.append(ele) \n\t\nmergeArrays(arr1, arr2, n, m)"}},"points":10,"difficulty":"easy","sampleInput":"4\r\n-2 \r\n5 \r\n9 \r\n11\r\n3\r\n4 \r\n6 \r\n8","sampleOutput":"-2\r\n4\r\n5\r\n6\r\n8\r\n9\r\n11","questionVideo":"https://www.youtube.com/embed/WMxNhIBA92I?end=50","hints":[],"associated":[{"id":"3fa0d88c-8e6b-4b51-b5f2-519650fed930","name":"If the size of arr1 is n and the size of arr2 is m, what will be the size of resultant array?","slug":"if-the-size-of-arr1-is-n-and-the-size-of-arr2-is-m-what-will-be-the-size-of-resultant-array","type":4},{"id":"6e337b9b-5c2f-4e2b-83f7-1e0e483a9e60","name":"When we're merging the two arrays, are the original arrays also being affected?","slug":"when-we-re-merging-the-two-arrays-are-the-original-arrays-also-being-affected","type":4},{"id":"9bbd8b0f-dac7-40bb-bc02-23da249aa7b6","name":"What does linear time complexity mean in Merge Two Sorted Arrays?","slug":"what-does-linear-time-complexity-mean-in-merge-two-sorted-arrays","type":4},{"id":"b208bade-6f26-49a0-9392-9c593c4274b6","name":"Find the resultant array:","slug":"find-the-resultant-array","type":4}],"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":"8e3abac7-a5ab-4090-abd1-b26ef8b53d70","name":"Time and Space Complexity","slug":"time-and-space-complexity","type":0},{"id":"1c4f0079-b3f5-4971-ba99-f1171ea09f9a","name":"Merge Two Sorted Arrays","slug":"merge-two-sorted-arrays","type":1}],"next":{"id":"40fa2cae-069b-4fed-a579-fb2860d12d49","name":"Merge Two Sorted Arrays","type":3,"slug":"merge-two-sorted-arrays"},"prev":{"id":"59db3665-b776-4b08-937c-3201108b635a","name":"Insertion Sort","type":3,"slug":"insertion-sort"}}}

Merge Two Sorted Arrays

1. You are given two sorted arrays(a,b) of integers. 2. You have to merge them and form one sorted array. 3. You have to do it in linear time complexity.

{"id":"b5a60c48-b108-42ba-a6d6-b1917d228ea5","name":"Merge Two Sorted Arrays","description":"1. You are given two sorted arrays(a,b) of integers.\r\n2. You have to merge them and form one sorted array.\r\n3. You have to do it in linear time complexity.","inputFormat":"An Integer n \r\na1\r\na2..n integers\r\nAn integer m\r\nb1\r\nb2..m integers","outputFormat":"Check the sample output and question video.","constraints":"1 &lt;= N &lt;= 10^8\r\n-10^9 &lt;= a[i],b[i] &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nvector<int> mergeTwoSortedArrays(vector<int> &A, vector<int> &B)\r\n{\r\n \r\n}\r\n\r\nvoid input(vector<int> &arr)\r\n{\r\n for (int i = 0; i < arr.size(); i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n}\r\n\r\nvoid output(vector<int> &arr)\r\n{\r\n for (int i = 0; i < arr.size(); i++)\r\n {\r\n cout << arr[i] << endl;\r\n }\r\n}\r\n\r\nint main()\r\n{\r\n int n, m;\r\n cin >> n;\r\n vector<int> A(n, 0);\r\n input(A);\r\n\r\n cin >> m;\r\n vector<int> B(m, 0);\r\n input(B);\r\n\r\n vector<int> ans = mergeTwoSortedArrays(A, B);\r\n output(ans);\r\n return 0;\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int[] mergeTwoSortedArrays(int[] a, int[] b){\r\n //write your code here\r\n \r\n return null;\r\n }\r\n\r\n public static void print(int[] arr){\r\n for(int i = 0 ; i < arr.length; i++){\r\n System.out.println(arr[i]);\r\n }\r\n }\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] a = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n a[i] = scn.nextInt();\r\n }\r\n int m = scn.nextInt();\r\n int[] b = new int[m];\r\n for(int i = 0 ; i < m; i++){\r\n b[i] = scn.nextInt();\r\n }\r\n int[] mergedArray = mergeTwoSortedArrays(a,b);\r\n print(mergedArray);\r\n }\r\n\r\n}"},"python":{"code":"def mergeArrays(arr1, arr2, n1, n2):\n\t# write your code\n \n\n\narr1= []\n\nn = int(input())\n\nfor i in range(0, n):\n\tele = int(input())\n\t\n\tarr1.append(ele)\n\t\narr2 =[]\nm = int(input())\n\nfor i in range(0, m):\n\tele = int(input())\n\n\tarr2.append(ele) \n\t\nmergeArrays(arr1, arr2, n, m)"}},"points":10,"difficulty":"easy","sampleInput":"4\r\n-2 \r\n5 \r\n9 \r\n11\r\n3\r\n4 \r\n6 \r\n8","sampleOutput":"-2\r\n4\r\n5\r\n6\r\n8\r\n9\r\n11","questionVideo":"https://www.youtube.com/embed/WMxNhIBA92I?end=50","hints":[],"associated":[{"id":"3fa0d88c-8e6b-4b51-b5f2-519650fed930","name":"If the size of arr1 is n and the size of arr2 is m, what will be the size of resultant array?","slug":"if-the-size-of-arr1-is-n-and-the-size-of-arr2-is-m-what-will-be-the-size-of-resultant-array","type":4},{"id":"6e337b9b-5c2f-4e2b-83f7-1e0e483a9e60","name":"When we're merging the two arrays, are the original arrays also being affected?","slug":"when-we-re-merging-the-two-arrays-are-the-original-arrays-also-being-affected","type":4},{"id":"9bbd8b0f-dac7-40bb-bc02-23da249aa7b6","name":"What does linear time complexity mean in Merge Two Sorted Arrays?","slug":"what-does-linear-time-complexity-mean-in-merge-two-sorted-arrays","type":4},{"id":"b208bade-6f26-49a0-9392-9c593c4274b6","name":"Find the resultant array:","slug":"find-the-resultant-array","type":4}],"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":"8e3abac7-a5ab-4090-abd1-b26ef8b53d70","name":"Time and Space Complexity","slug":"time-and-space-complexity","type":0},{"id":"1c4f0079-b3f5-4971-ba99-f1171ea09f9a","name":"Merge Two Sorted Arrays","slug":"merge-two-sorted-arrays","type":1}],"next":{"id":"40fa2cae-069b-4fed-a579-fb2860d12d49","name":"Merge Two Sorted Arrays","type":3,"slug":"merge-two-sorted-arrays"},"prev":{"id":"59db3665-b776-4b08-937c-3201108b635a","name":"Insertion Sort","type":3,"slug":"insertion-sort"}}}
plane

Editor


Loading...

Merge Two Sorted Arrays

easy

1. You are given two sorted arrays(a,b) of integers. 2. You have to merge them and form one sorted array. 3. You have to do it in linear time complexity.

Constraints

1 <= N <= 10^8 -10^9 <= a[i],b[i] <= 10^9

Format

Input

An Integer n a1 a2..n integers An integer m b1 b2..m integers

Output

Check the sample output and question video.

Example

Sample Input

4 -2 5 9 11 3 4 6 8

Sample Output

-2 4 5 6 8 9 11

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode