Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity analysis for finding the maximum element

I've encountered a homework problem:

which of these is an asymptotically tight upper bound for the best-case running time of an optimal algorithm that finds the maximum element in an arbitrary array of integers of size n

  1. O(log n)
  2. O(n2)
  3. O(n)
  4. O(1)
  5. O(n log n)

Based on my understanding, it's O(n) since even it's the best-case we still need to scan over the arr to get the result. Please correct me

like image 466
Fihop Avatar asked Jun 12 '15 21:06

Fihop


People also ask

What is the time complexity to get the maximum?

This argument shows that any deterministic algorithm that always finds the maximum value in an array must look at all positions in that array, so the runtime must be Ω(n) to be correct. Hope this helps!

What is the best case time complexity of finding the maximum element in an array?

We are given an integer array of size N or we can say number of elements is equal to N. We have to find the largest/ maximum element in an array. The time complexity to solve this is linear O(N) and space compexity is O(1).

What is the complexity of finding maximum and minimum value from an array of n values explain the steps of deriving complexity?

Return max and min. Time Complexity is O(n) and Space Complexity is O(1). For each pair, there are a total of three comparisons, first among the elements of the pair and the other two with min and max.


2 Answers

Yes, that's correct. One way to see this is via an adversarial argument. Suppose that you have an algorithm that allegedly finds the maximum value in the array, but doesn't inspect every array element at least once.

Suppose I run your algorithm on some array A1 that consists of nothing but the number 0. Since your algorithm doesn't look at all positions in the array, there's some position it doesn't look at; call that position k. Now, define A2 to be an array that's the same as array A1, except that the element at position k in A2 is defined to be 1.

Now, what happens if I run your algorithm on A2? Since your algorithm never looked at position k in A1 and A2 is identical to A2 except at position k, your algorithm can't tell A1 and A2 apart. Therefore, whatever it says for A1 must be the same as what it says for A2.

That's a problem, though. If your algorithm says that the maximum value is 0, then it's wrong for A2, whose max is 1. If your algorithm says that the maximum value is 1, then it's wrong for A1, whose max is 0. Therefore, the algorithm has to be wrong in at least one case, so it can't be a correct algorithm for finding the maximum value.

This argument shows that any deterministic algorithm that always finds the maximum value in an array must look at all positions in that array, so the runtime must be Ω(n) to be correct.

Hope this helps!

like image 80
templatetypedef Avatar answered Sep 25 '22 07:09

templatetypedef


O(n) is the running time if we know nothing about the data in the array. Which in your case is true. "an arbitrary array of integers of size n" implies that it could be any integer array.

O(1) is possible when the array is sorted. O(nlog n) is possible if we first use quicksort to sort the array and then select the largest item. O(n^2) is possible if we first sort the array using bubblesort and then just select the largest item.

like image 45
Jaspreet Singh Avatar answered Sep 23 '22 07:09

Jaspreet Singh