C | Arrays | Question 5
Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n + m] be another integer array.
Which of the following condition(s) hold(s) after the termination of the while loop? (GATE CS 2006) (i) j < m, k = n+j-1, and a[n-1] < b[j] if i = n (ii) i < n, k = m+i-1, and b[m-1] <= a[i] if j = m
either (i) or (ii) but not both
neither (i) nor (ii)
The function xyz() is similar to merge() of mergeSort().Both the conditions (i) and (ii) are false in the sections k = n+j-1 and k = m+i-1 respectively. The while loop adds elements from a and b (whichever is smaller) to c and terminates when either of them exhausts. So, when the loop terminates either i=n or j=m. Suppose i=n. This would mean all elements from array a are added to c=>k must be incremented by n. c would also contain j elements from array b. So, the number of elements in c would be n+j and hence k=n+j. Similarly, when j=m, k=m+i. Hence, the option (D) is correct. (Had k started from −1 and not 0 and we used ++k inside the loop, the answer would have been option (C))
Quiz of this Question
Please comment below if you find anything wrong in the above post
Please Login to comment...