retagged by
7,781 views
0 0 votes

. You are given a set of n points on the number line. They are given in arbitrary order. The task is to find the points that are closest to each other.

To solve the problem you decide to take one point and compute its distance to all other points and repeat this process for all points. During the process you track the closest pair.

What is the running time of this algorithm ?

 

  1. O(nlgn)
  2. O(n^2)
  3. Runtime is independent of n.
  4. O(log n)

1 Answer

1 1 vote

The answer is B $O(n^2)$.

The reasoning behind this is that you essentially calculate the distance between all the pairs of points. Assuming there are n numbers, you calculate the distance (based on this algorithm) for $n \dfrac{(n-1)}{2}$ pairs, hence $O(n^2)$.

 

Since all numbers are on the number line, this can be done in a more efficient way, i.e. sort the points in the line and the traverse the list of points to find the closest. 

Position:
Show:

Related questions

2 2 votes
1 answers 1 answer
1.3k
1.3k views
rsansiya111 asked Dec 8, 2021
1,338 views
Suppose we do merge sort with a three-way split: divide the array into 3 equal parts, sort each part and do a 3 way merge.What would the worst-case complexity of this ver...
0 0 votes
1 1 answer
2.2k
2.2k views
rsansiya111 asked Dec 8, 2021
2,151 views
Suppose we want to extend the union-find data structure to support the operation Reset(c), which takes as input the name of a component c and then breaks up c into single...
0 0 votes
3 3 answers
1.7k
1.7k views
rsansiya111 asked Dec 8, 2021
1,721 views
Suppose there are k sorted lists (decreasing order) with n/k elements in each list.What is the time complexity to merge them into one single sorted list.Hint: Maintain a ...
0 0 votes
1 1 answer
582
582 views