Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Sourav Singh
Requesting the time and space complexity of below code?

Sourav Singh

Oct 9, 2024

func (s *Solution) printMax(arr []int, k int) []int { result := []int{} queue := []int{} for i := 0; i < len(arr); i++ { if len(queue) > 0 && queue[0] < i-k+1 { queue = queue[1:] } for len(queue) > 0 && arr[queue[len(queue)-1]] < arr[i] { queue = queue[:len(queue)-1] } queue = append(queue, i) if i >= k-1 { result = append(result, arr[queue[0]]) } } return result }

0

0

Comments
Comments
Shubham Vora
Shubham Voraa year ago

Time Complexity Analysis:

  1. Outer Loop (for i := 0; i < len(arr); i++):

    • The outer loop runs once for each element in the input array arr. Therefore, its time complexity is O(n), where n is the length of the array.
  2. **Removing Elements fr...

On this page