• edited by
1,202 views
3 3 votes
Consider two arrays A[] and B[],if arrays A is in increasing order and array B is in decreasing order is input to join a algorithm. the output is an array C[1......2n] which has all the values of the arrays A[]  and B[] and is increasing order . what is the worst case time complexity for join algorithm to join two array?

A) O(n2)

B)O(n)

C)O(1)

D)O(nlogn)

1 Answer

Best answer
3 3 votes

We can do it in O(n).

It is similar to the MERGE procedure, where we merge two sorted arrays into one sorted array. The difference is that now array B is in reverse order. So, instead of starting indexing B from 0 and incrementing its index whenever we insert an element from B to C, we can start indexing B from n-1, and keep decrementing it whenever we insert an element from B to C, until it reaches 0.

Just some small changes to MERGE procedure will do the work.

• selected by
Position:
Show:

Related questions

5 5 votes
1 answers 1 answer
1.6k
1.6k views
balchandar reddy san asked Jan 30, 2019
1,567 views
Consider a new sorting algorithm similar to the BubbleSort algorithm, called RumbleSort. Given an array as input, RumbleSort attempts to sort the array and produces a sor...
1 1 vote
1 1 answer
1.7k
1.7k views
eyeamgj asked Jan 29, 2018
1,714 views
Given two unsorted singly-linked lists each with n distinct elements. There exists an efficient intersection algorithm, that computes and returns a new list with common e...
1 1 vote
1 answers 1 answer
866
866 views
1 1 vote
1 1 answer
1.8k
1.8k views
srestha asked Apr 28, 2019
1,765 views
Given a sorted array of distinct integer $A\left [ 1,2,....n \right ]$, the tightest upper bound to check the existence of any index $i$, for which $A[i]=i$ is equal to _...