Home > Back-end >  Time complexity (How is this O(n))
Time complexity (How is this O(n))

Time:06-28

I am having trouble understanding how this code is O(N). Is the inner while loop O(1). If so, why? When is a while/for loop considered O(N) and when is it O(1)?

int minSubArrayLen(int target, vector& nums)
{

   int left=0;
    int right=0;
    int n=nums.size();
    int sum=0;
    int ans=INT_MAX;
    int flag=0;
    while(right<n)
    {
        sum =nums[right];
        if(sum>=target)
        {
            while(sum>=target)
            {
                flag=1;

                sum=sum-nums[left];
                left  ;
            }
        ans=min(ans,right-left 2);
        }
        right  ;
    }
   if(flag==0)
   {
       return 0;
   }
    return ans;
}
};

CodePudding user response:

Both the inner and outer loop are O(n) on their own.

But consider the whole function and count the number of accesses to nums:

The outer loop does:

    sum =nums[right];
    right  ;

No element of nums is accessed more than once through right. So that is O(n) accesses and loop iterations.

Now the tricky one, the inner loop:

            sum=sum-nums[left];
            left  ;

No element of nums is accessed more than once through left. So while the inner loop runs many times in their sum it's O(n).

So overall is O(2n) == O(n) accesses to nums and O(n) runtime for the whole function.

CodePudding user response:

Outer while loop is going from 0 till the n so time complexity is O(n).

O(1): int sum= 0; for(int x=0 ; x<10 ; x ) sum =x; Every time you run this loop, it will run 10 times, so it will take constant time . So time complexity will be O(1).

O(n): int sum=0; For(int x=0; x<n; x ) sum =x; Time complexity of this loop would be O(n) because the number of iterations is varying with the value of n.

CodePudding user response:

Consider the scenario

The array is filled with the same value x and target (required sum) is also x. So at every iteration of the outer while loop the condition sum >= target is satisfied, which invokes the inner while loop at every iterations. It is easy to see that in this case, both right and left pointers would move together towards the end of the array. Both the pointers therefore move n positions in all, the outer loop just checks for a condition which calls the inner loop. Both the pointes are moved independently.

You can consider any other case, and in every case you would find the same observation. 2 independent pointers controlling the loop, and both are having O(n) operations, so the overall complexity is O(n).

CodePudding user response:

O(n) or O(1) is just a notation for time complexity of an algorithm.

  • O(n) is linear time, that means, that if we have n elements, it will take n operations to perform the task.

  • O(1) is constant time, that means, that amount of operations is indifferent to n.

It is also worth mentioning, that your code does not cover one edge case - when target is equal to zero.

Your code has linear complexity, because it scans all the element of the array, so at least n operations will be performed.

Here is a little refactored code:

int minSubArrayLen(int target, const std::vector<int>& nums) {
    int left = 0, right = 0, size = nums.size();
    int total = 0, answer = INT_MAX;
    bool found = false;
    
    while (right < size) {
        total  = nums[right];

        if (total >= target) {
            found = true;
            while (total >= target) {
                total -= nums[left];
                  left;
            }

            answer = std::min(answer, right - left   2);
        }

          right;
    }

    return found ? answer : -1;
}
  • Related