Solution to the time complexity


O(n) will be right.
let’s take 2 cases

  1. the array with increasing values: For this as the j=0 condition is given above the ‘for’ loop, once the value of J reaches n-1, it wont be back to 0 so, it won’t run further.
  2. the array with non-increasing(all equal or decreasing) : Here the for loop will run and check the while loop for each value of the array as the while loop wont be executed.


And when it is both increasing and decreasing


it doesn’t matter if its both increasing or decreasing the inner loop will always run once along with the outer loop, so solution is technically O(N+N) = O(N)