tag:blogger.com,1999:blog-2682781734368178743.post284463428017531536..comments2023-03-28T14:19:44.264-07:00Comments on Technical Interview Questions, Answers, and Tips: Merge 2 Sorted Arrays (one has empty slots)Unknownnoreply@blogger.comBlogger7125tag:blogger.com,1999:blog-2682781734368178743.post-53527296957758972692012-06-14T11:21:00.769-07:002012-06-14T11:21:00.769-07:00public int[] merge(int[] l, int[] r){
int li =...public int[] merge(int[] l, int[] r){<br /> int li = 0, ri = 0, index = 0;<br /> int[] result = new int[l.length + r.length];<br /> while(index < (l.length + r.length)){<br /> if(ri >= r.length || ((li < l.length) && (l[li] <= r[ri]))){<br /> result[index] = l[li];<br /> li++;<br /> }else{<br /> result[index] = r[ri];<br /> ri++;<br /> }<br /> Anonymoushttps://www.blogger.com/profile/09570733443665098048noreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-86903016014177248742011-06-22T00:47:26.026-07:002011-06-22T00:47:26.026-07:00// Both the above code doesn't work and produc...// Both the above code doesn't work and produces invalid result. Here is the correct code and works perfectly for the above example.<br /><br /> void mergeArray(int[] A1, int[] A2)<br /> {<br /> int count = FindCount(A2); // get the count of full slots<br /> int i = A1.Length - 1;<br /> int j = count - 1;<br /> int k = A2.Length - 1;<thiruvnoreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-45593137783839696872010-09-08T05:29:15.840-07:002010-09-08T05:29:15.840-07:00void Merge(int[] A1, int[] A2)
{
int count = Fi...void Merge(int[] A1, int[] A2)<br />{<br /> int count = FindCount(A2); // get the count of full slots<br /> int i = A1.Length - 1;<br /> int j = count - 1;<br /> int k = A2.Length - 1;<br /><br /> for(;k>=0;k--)<br /> {<br /> if(A1[i] > A2[j] || j < 0)<br /> {<br /> A2[k] =A1[i];<br /> i--;<br /> if(i<0)<br /> break;<br /> }<Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-30418644210469062452010-08-12T08:09:56.498-07:002010-08-12T08:09:56.498-07:00void merge(int[] A1, int[] A2)
{
int count = fi...void merge(int[] A1, int[] A2)<br />{<br /> int count = findCount(A2); // get the count of full slots<br /> int i = A1.length - 1;<br /> int j = count - 1;<br /> int k = A2.length - 1;<br /><br /> for(;k>=0;k--)<br /> {<br /> if((i >= 0 && j >= 0 && A1[i] > A2[j]) || j < 0)<br /> {<br /> A2[k] =A1[i];<br /> i--;<br /> }<br Bipin Jethwanihttps://www.blogger.com/profile/16544987540436372739noreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-85505508055304665472010-01-21T18:37:37.437-08:002010-01-21T18:37:37.437-08:00Have you tried running this code ? I think it will...Have you tried running this code ? I think it will give ArrayIndexOutofBounds (or similar exception) on line:<br />if(A1[i] > A2[j] || j < 0)geekhttp://techpuzzl.wordpress.com/noreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-13592500958749291792009-10-26T19:47:00.885-07:002009-10-26T19:47:00.885-07:00thanks for the feedback. I revised the problem des...thanks for the feedback. I revised the problem description and the solution.gmannoreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-17576049297084519112009-10-25T17:08:53.322-07:002009-10-25T17:08:53.322-07:00i think there is a mistake in the index for smalle...i think there is a mistake in the index for smaller, it should be j instead of i.Anonymousnoreply@blogger.com