Use sliding window with hashmap tracking frequencies; expand right pointer, shrink when distinct elements exceed K, update maximum subarray sum continuously during traversal.
1. Longest Increasing Subsequence (LIS)
Use dynamic programming or binary search with a tails array; iterate through elements, update positions using lower_bound, achieving O(n log n) time complexity for efficient subsequence length calculation.
2. Detect Cycle in Directed Graph
Use DFS with recursion stack or Kahn’s algorithm; track visited and path nodes, if revisited in recursion stack, cycle exists, otherwise graph remains acyclic during traversal.