Hello,
Can someone help with the this question,
How to find the minumum absolute diff among the last k numbers from incoming stream of integers.
Example: arr = [3,8,-10,23,19,-4,-14,27], k = 3
Ans = 10, since K = 3, last 3 elements [-4, -14, 27], abs min diff among these is 10.
min absolute diff in fixed array would take O(n logn) time with sorting and single traversal for consecutive comparison
If the input is incoming stream of integers, I would store incoming numbers fixed storage of queue of size k, (this would help us to know which elements are added and remove) since we are interested in last k numbers in the incoming stream of numbers and also store last k numbers in a Binary search tree(BST) for doing the consecutive comparisons in the sorted order.
An enqueue into queue tells this needs to added to BST and dequeued number to be removed from the BST. and modifered inorder travsersal on this BST gives the min absolute diff for the last K numbers.
Do you guys know if there is anyother better approach ?