1,648 views
1 votes
1 votes

<!--StartFragment -->

void fun(int n, int arr[])

{

    int i = 0, j = 0;

    for(; i < n; ++i)

        while(j < n && arr[i] < arr[j])

            j++;

}

 

In this question the inner loop runs n times at most because j is not initialised for every I. this makes complexity O(n) but wont the outer loop also run n times (incrementing and checking conditions in while) and making the total time complexity to be O(n2)??

<!--EndFragment -->

2 Answers

1 votes
1 votes

Once value of J =n , innermost loop will not be executed. thus total  n + n-1 times the two loops run  - thus TC =  O(n)

Related questions

0 votes
0 votes
3 answers
2
Nisha Bharti asked Sep 26, 2022
751 views
What is the time & space complexity of this algorithm?Main(){ for(i=n; i>10; i=i^1/4) { for(j=201; j<n^3; j=j+400) ...
0 votes
0 votes
1 answer
3
tusharb asked Feb 18, 2022
711 views
As we know the time complexity of solving the greedy knapsack algorithm depends mainly on the sorting algorithm used, Can we use counting sort as the sorting algorithm to...
1 votes
1 votes
0 answers
4
srestha asked May 19, 2019
639 views
Let $A(n)$ denotes the number of $n$ bit binary strings which have no pair of consecutive $1’s.$ what will be recurrence relation for it and what will be it’s Time Co...