{"id":25342,"date":"2017-10-15T17:12:36","date_gmt":"2017-10-15T11:42:36","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25342"},"modified":"2017-10-15T17:12:36","modified_gmt":"2017-10-15T11:42:36","slug":"find-common-elements-three-sorted-arrays-3","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/find-common-elements-three-sorted-arrays-3\/","title":{"rendered":"PYTHON Programming-Find common elements in three sorted arrays"},"content":{"rendered":"<p>Given three arrays sorted in non-decreasing order, print all common elements in these arrays.<\/p>\n<p>Examples:<\/p>\n<p>ar1[] = {1, 5, 10, 20, 40, 80}<br \/>\nar2[] = {6, 7, 20, 80, 100}<br \/>\nar3[] = {3, 4, 15, 20, 30, 70, 80, 120}<br \/>\nOutput: 20, 80<\/p>\n<p>ar1[] = {1, 5, 5}<br \/>\nar2[] = {3, 4, 5, 5, 10}<br \/>\nar3[] = {5, 5, 10, 20}<br \/>\n<strong>Output:<\/strong> 5, 5<br \/>\nA simple solution is to first find intersection of two arrays and store the intersection in a temporary array, then find the intersection of third array and temporary array. Time complexity of this solution is O(n1 + n2 + n3) where n1, n2 and n3 are sizes of ar1[], ar2[] and ar3[] respectively.<\/p>\n<p>The above solution requires extra space and two loops, we can find the common elements using a single loop and without extra space. The idea is similar to intersection of two arrays. Like two arrays loop, we run a loop and traverse three arrays.<br \/>\nLet the current element traversed in ar1[] be x, in ar2[] be y and in ar3[] be z. We can have following cases inside the loop.<br \/>\n1) If x, y and z are same, we can simply print any of them as common element and move ahead in all three arrays.<br \/>\n2) Else If x &lt; y, we can move ahead in ar1[] as x cannot be a common element 3) Else If y &lt; z, we can move ahead in ar2[] as y cannot be a common element 4) Else (We reach here when x &gt; y and y &gt; z), we can simply move ahead in ar3[] as z cannot be a common element.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following are implementations of the above idea.<\/p>\n<p><strong>Python Programmming<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">python<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python function to print common elements in three sorted arrays<br\/>def findCommon(ar1, ar2, ar3, n1, n2, n3):<br\/>     <br\/>    # Initialize starting indexes for ar1[], ar2[] and ar3[]<br\/>    i, j, k = 0, 0, 0<br\/>     <br\/>    # Iterate through three arrays while all arrays have elements    <br\/>    while (i &lt; n1 and j &lt; n2 and k&lt; n3):<br\/>         <br\/>        # If x = y and y = z, print any of them and move ahead <br\/>        # in all arrays<br\/>        if (ar1[i] == ar2[j] and ar2[j] == ar3[k]):<br\/>            print ar1[i],<br\/>            i += 1<br\/>            j += 1<br\/>            k += 1<br\/>         <br\/>        # x &lt; y    <br\/>        elif ar1[i] &lt; ar2[j]:<br\/>            i += 1<br\/>             <br\/>        # y &lt; z    <br\/>        elif ar2[j] &lt; ar3[k]:<br\/>            j += 1<br\/>         <br\/>        # We reach here when x &gt; y and z &lt; y, i.e., z is smallest    <br\/>        else:<br\/>            k += 1<br\/> <br\/># Driver program to check above function<br\/>ar1 = [1, 5, 10, 20, 40, 80]<br\/>ar2 = [6, 7, 20, 80, 100]<br\/>ar3 = [3, 4, 15, 20, 30, 70, 80, 120]<br\/>n1 = len(ar1)<br\/>n2 = len(ar2)<br\/>n3 = len(ar3)<br\/>print &quot;Common elements are&quot;,<br\/>findCommon(ar1, ar2, ar3, n1, n2, n3)<br\/> <br\/># This code is contributed by __Devesh Agrawal__<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>PYTHON Programming-Find common elements in three sorted arrays &#8211; Searching and sorting &#8211; A simple solution is to first find intersection of two arrays and store the intersection in a temporary array, then find the intersection of third array and temporary array. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,83517,71670],"tags":[70235,72892,72869,72900,72899,72887,72903,72696,72877,72908,72819,72890,72891,72911,72907,72870,72876,72879,72918,72895,72874,72883,72894,72916,72915,72917,70891,72898,72989,72909,72886,72888,72896,72913,72872,72897,72990,72871,72880,72875,72901,72893,72904,72889,72910,72878,72884,72882,72873,72881,72914,72912,72885],"class_list":["post-25342","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-python-programming","category-searching-and-sorting","tag-algorithm-examples","tag-algorithm-to-find-largest-of-n-numbers","tag-c-programming-kth-smallestlargest-element-in-unsorted-array","tag-cluster-random-sampling","tag-element-n","tag-find-kth-smallest-element-in-array-java","tag-find-kth-smallest-element-in-two-sorted-arrays","tag-find-largest-number-in-array-java","tag-find-max-value-in-array-java","tag-find-median","tag-find-minimum-value-in-array-java","tag-find-smallest-number-in-array","tag-find-smallest-number-in-array-java","tag-find-the-mean","tag-find-the-median","tag-k-element","tag-k-sel","tag-kth-element","tag-kth-number","tag-kth-smallest-element-in-array","tag-largest-element","tag-largest-element-in-an-array","tag-least-element","tag-max-value-in-array-java","tag-median-of-two-sorted-arrays","tag-methods-of-sampling-in-statistics","tag-min-heap","tag-population-vs-sample","tag-python-programming-kth-smallestlargest-element-in-unsorted-array","tag-quick-select","tag-sample-in-statistics","tag-sample-statistic","tag-sample-statistics","tag-sample-vs-population","tag-sampling-techniques-in-statistics","tag-sampling-types","tag-select-max-n-element","tag-smallest-element","tag-statistical-sampling","tag-statistics-examples","tag-systematic-random-sampling","tag-systematic-sampling","tag-systematic-sampling-definition","tag-th-element","tag-the-smallest-element","tag-tj-maxx","tag-types-of-sampling","tag-types-of-sampling-methods","tag-types-of-sampling-techniques","tag-types-of-statistics","tag-unsorted","tag-what-is-a-stratified-sample","tag-what-is-systematic-sampling"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25342"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25342\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}