void fun(int n, int arr[])
{
int i = 0, j = 0;
for(; i < n; ++i)
while(j < n && arr[i] < arr[j])
j++;
}
The answer given is: variable j is not initialized for each value of variable i, so time complexity is O(n)
I don't quite understand it. Can anyone explain?
The outer loop is executed
n
times.The inner loop only continues if
j < n
, and sincej
is incremented in each step, it cannot be executed more thann
times in total.