Small correction

Similarly, when j = m, k = **n** + i.

Change n to m.

The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+18 votes

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,

void xyz(int a[], int b [], int c []){ int i,j,k; i=j=k=0; while ((i<n) && (j<m)) if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; }

Which of the following condition(s) hold(s) after the termination of the while loop?

- $j< m,k=n+j-1$ and $a[n-1]< b[j]$ if $i=n$
- $i<n,k=m+i-1$ and $b[m-1]\leq a[i]$ if $j=m$

- only (i)
- only (ii)
- either (i) or (ii) but not both
- neither (i) nor (ii)

+44 votes

Best answer

The while loop adds elements from $a$ and $b$ (whichever is smaller) to $c$ and terminates when either of them exhausts. So, when 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, number of elements in $c$ would be $n+j$ and hence $k = n + j$.

Similarly, when $j = m$, $k = m + i$.

Hence, option (**D**) is correct. (Had $k$ started from $-1$ and not $0$ and we used $++k$ inside loop, answer would have been option (C))

0

@arjun sir . if we take same element in same index of both array . we can come to solution directly.

i took A[ 12 3] and B [1259] :).

Its very hard for me to interpret the code by seeing :(

i took A[ 12 3] and B [1259] :).

Its very hard for me to interpret the code by seeing :(

+17 votes

By Option Elimination

Take Array Contents

a={1} b={2} so n=1,m=1

Now A is small so it will copy to C then terminate in next iteration cz i<n no more holds So content of variables after while loop

c={1} , i=1,j=0,k=1,n=1,m=1

Check Condition i :->> (i==n) Yes. j<m holds but k=n+j-1 does not hold ,So Condition i is false.

Now Take Array Contents

a={2} b={1} so n=1,m=1

Now B is small so it will copy to C then terminate in next iteration cz j<m no more holds So content of variables after while loop

c={1} , i=0,j=1,k=1,n=1,m=1

Check Condition ii :->> (j==m) Yes. i<n holds but k=m+i-1 does not hold ,So Condition ii is false.

Neither condition holds Hence Option D is correct Ans.

Take Array Contents

a={1} b={2} so n=1,m=1

Now A is small so it will copy to C then terminate in next iteration cz i<n no more holds So content of variables after while loop

c={1} , i=1,j=0,k=1,n=1,m=1

Check Condition i :->> (i==n) Yes. j<m holds but k=n+j-1 does not hold ,So Condition i is false.

Now Take Array Contents

a={2} b={1} so n=1,m=1

Now B is small so it will copy to C then terminate in next iteration cz j<m no more holds So content of variables after while loop

c={1} , i=0,j=1,k=1,n=1,m=1

Check Condition ii :->> (j==m) Yes. i<n holds but k=m+i-1 does not hold ,So Condition ii is false.

Neither condition holds Hence Option D is correct Ans.

- All categories
- General Aptitude 1.3k
- Engineering Mathematics 5.4k
- Digital Logic 2.1k
- Programming & DS 3.9k
- Algorithms 3.4k
- Theory of Computation 4.2k
- Compiler Design 1.6k
- Databases 3.1k
- CO & Architecture 2.7k
- Computer Networks 3.1k
- Non GATE 1.1k
- Others 1.4k
- Admissions 501
- Exam Queries 447
- Tier 1 Placement Questions 19
- Job Queries 62
- Projects 12

37,939 questions

45,453 answers

131,194 comments

48,209 users