explain!
Worst case is not finding the equivalent element.
[ 3 , 5 , 4 , 5 , 8 , 9, 1]
The algorithm works by, compairing a each element with all the array elements.
for( i = 0; i < n ; i++)
for (j = 0; j < n ; j++)
if ( i != j && arr[i] == arr[j] )
break;
So, here, first loop runs for n times, second loop runs for n times for each n of first loop. Since the quest is about time complexity, space complexity can be ignored.
So, total iterations this loop would run is n*n = n^2 , O(n^2) should be the answer. Correct me if i'm wrong.
@Althaf.
Rather than comparing, I would sort the array and then the elemrnts would be adjacent.
So, O(nlogn + n) = O(n.logn).
But since, there is no memory constraint (since it would have been given), It can be done by hashing in O(n) or worst O(n.logn).
64.3k questions
77.9k answers
244k comments
80.0k users