educative.io

Is my solution Time Complexity O(n) but not accurate due to creating new array?

Hi for my solution I’d like to understand if this is not good because I’m not doing the sliding window in-situ? Otherwise my time complexity is O(n) isn’t it?

 def max_sub_array_of_size_k(k, arr):
      n = len(arr)
      arr_sum = []
      for i in range(n):
        arr_sum.append(sum(arr[i:i+k]))
      return max(arr_sum)

def main(): 
  out = max_sub_array_of_size_k(3,[2, 1, 5, 1, 3, 2])
  print(out)

main()

Hey @John_R

Your code is fine, as there is only a single loop in your code so the time complexity is O(n).

Shaheryaar Kamal | Technical Content Engineer

Thanks @Shaheryaar_Kamal, appreciate the reply back.