679 views
1 votes
1 votes

What is the worst case time complexity of an efficient algorithm (in order of n) to get the last index for an actually filled element, in an array, given the condition that we may not fill the entire initialized array with elements, the array is initialized as “int a[n]; ” [Array may not be filled in a sorted order]

  1. O(n)
  2. O(1)
  3. O(log(n))
  4. O($n^2$)

 

1 Answer

0 votes
0 votes
The worst case time complexity of an efficient algorithm to get the last index for an actually filled element in an array would be O(n). This is because you would have to check each element in the array one by one until you find the last element that has been filled. You cannot do better than this because you have to check all the elements in the worst case scenario. If the array is not filled in a sorted order, you cannot use a binary search or any other algorithm with a lower time complexity because the array is not sorted and you do not have any other information about the distribution of the elements in the array.

Related questions

0 votes
0 votes
1 answer
1
iarnav asked Jun 12, 2018
881 views
Given an sorted array in descending order, what will be the time complexity to delete the minimum element from this array?
0 votes
0 votes
3 answers
2
Subham Nagar asked Mar 20, 2018
1,254 views
An array $'A'$ has $n$ distinct integers. What is the tightest time complexity to check $A[i]=i$ for some $i$. Consider all elements of array within range from $1$ to $n$...
1 votes
1 votes
0 answers
4
srestha asked Jan 13, 2017
422 views
An array is of size n. Find the time complexity to insert a new value before an element e in the array?(A) O(N)(B) O(log N)(C) O(1)(D) O(N2)