educative.io

How can we calculate time complexity of this problem?

import java.util.*;

public class SumOfThree{

public static boolean findSumOfThree(int[] nums, int target) {

  int left = 0; int right= nums.length-1;

  int i = left + 1;

  Arrays.sort(nums);
 int sum = 0;

  while(left < right){

     for(i= left+1;i< right;i++){

         sum = nums[left] + nums[right] + nums[i];

      if(target == sum){

        return true;

      }

     

     }

     if (sum < target){

        left++;

     }else if(sum > target){

        right--;

     }

   

     }

 

  return false;

}

}


Course: Grokking Coding Interview Patterns in Java - Learn Interactively
Lesson: Sum of Three Values - Grokking Coding Interview Patterns in Java

The time complexity of this code will be O(n^2). The outer while loop and inner for loop will both work O(n) times in the worst case leaving us with a complexity of O(n^2).