Arrays Flashcards
1
Q
Search in rotated sorted array
A
- Binary search, compare start with mid to find ascending or descending
- Then check whether the target falls into the range
2
Q
Duplicate element in 0 - n -1 array
A
- Use tortoise and hare algo
- Find the cycle using do while and equality
- Set tortoise to start and iterate both by 1 and find the intersection
Hashset and this algo are same time complexity, but this requires O(1) space complexity
3
Q
Sliding window maximum
A
- Use queue to maintain the order of insertion (of indexes)
- Remove the out of index by i - k + 1 with peek value
- Remove the indexes with lower value than the current index