893 views
11 11 votes

Let $S$ be a sorted array of $n$ distinct positive integers. Let $T(n)$ denote the time complexity of the most efficient algorithm to determine if there exist three indices $i<j<k$ such that the condition $S[i]+S[j]>S[k]$ is met.
 
Which of the following is true?

  1. $T(n)=O\left(n^3\right)$
     
  2. $T(n)=O\left(n^2\right)$
     
  3. $T(n)=O(n \log n)$
     
  4. $T(n)=O(n)$

2 Answers

2 2 votes
indireclty the question is speakig about the insertion sorting . In insertion sorting if the array is sorted  there will be no inversions(swappings)  , so here we will have only time for comparisions is O(n).

n comparisions+ 0 inversions=O(n)
1 1 vote
Loop with an index $i$ from 0 to $n-3$. In each step, check if $S[i]+S[i+1]>S[i+2]$. If this condition is ever met, you can immediately stop because you've found the answer. If the loop completes without finding such a triplet, then no such combination exists.

This single pass through the array results in a time complexity of $O(n)$.
Answer:
Position:
Show:

Related questions

5 5 votes
2 2 answers
848
848 views
GO Classes asked Oct 6, 2025
848 views
An algorithm performs a linear search for an element $k$ in an integer array of size $N$. The algorithm iterates through the array sequentially, starting from the first e...
6 6 votes
2 2 answers
1.3k
1.3k views
GO Classes asked Oct 6, 2025
1,309 views
Suppose four characters A, B, C, D have the frequencies $15, 8, 6,$ and $5$, respectively. After constructing the optimal Huffman code for this alphabet, what is the tota...
5 5 votes
3 3 answers
1.2k
1.2k views
GO Classes asked Oct 6, 2025
1,249 views
A project manager has broken down a project into $8$ tasks: $A, B, C, D, E, F, G,$ and $H$. The dependencies between the tasks are shown in the directed graph below. A va...
6 6 votes
2 2 answers
687
687 views
GO Classes asked Oct 6, 2025
687 views
If one uses the binary exponentiation (exponentiation by squaring) method to compute $a^{55}$, which of the following intermediate powers of $a$ is calculated but NOT use...