Given an initial list along with another list of numbers to be appended with the initial list and an integer K, return an array consisting of the Kth largest element after adding each element from the first list to the second list.
Example
Input: [ 2, [4, 6], [5, 2, 20] ]
Output: [5, 5, 6]
Notes
Constraints:
We have provided two solutions.
We will start with a brute-force approach that solves the problem in polynomial time complexity, later we present an optimized linear time complexity solution. We will refer to the initial list as intial_stream and the second list as append_stream.
The idea is to follow the exact same steps described in the problem statement.
for i = 1 to size_of_append_stream:
– Â Add append_stream[i] to initial_stream.
– Â Sort initial_stream in non-decreasing order.
– Â The Kth element from the end will be the Kth largest element.
Time Complexity:
O(|append_stream| * (|append_stream| + |initial_stream|) * log(|append_stream| + |initial_stream|)).
To sort the initial stream = O(|initial_stream| * log(|initial_stream|)).
To add an element from append_stream to the sorted stream = O((|append_stream| + |initial_stream|) * log(|append_stream| + |initial_stream|)).
We add |append_stream| number of elements to the sorted stream.
Auxiliary Space Used:
O(|append_stream|).
Memory used to add elements from append_stream to initial_stream = O(|append_stream|).
Space Complexity:
O(|initial_stream| + |append_stream|).
Memory used for input = O(|initial_stream| + |append_stream|).
Memory used for output = O(|append_stream|).
Auxiliary space = O(|append_stream|).
Total space complexity = O(|initial_stream| + |append_stream|).
// -------- START --------
vector kth_largest(int k, vector initial_stream, vector append_stream)
{
  vector result;
  for(int i = 0; i < append_stream.size(); i++) {
    initial_stream.push_back(append_stream[i]);
    sort(initial_stream.begin(), initial_stream.end());
    // kth element from the end will be the kth largest element.
    result.push_back(initial_stream[initial_stream.size() - k]);
  }
  return result;
}
// -------- END --------
The idea is to keep track of only the K largest elements of the stream. Create a min-heap storing the K largest elements from initial_stream. To create such a heap, we can sort the inital_stream and push K largest elements in the heap but this will require O(|inital_stream| * log(|inital_stream|)). Instead, we will directly push elements from inital_stream to min-heap to achieve a complexity of O(|inital_stream| * log(K)). While pushing the elements we will keep a check such that the size of min-heap does not exceed K which can be achieved by popping the top element of the heap when its size becomes (K + 1).
– If the new element is smaller than the top element of the heap, ignore it
– else remove the topmost element of the heap and insert the new element in the heap. To remove or insert a new element, the time complexity is O(log(K)).
The top element of the heap is always the Kth largest element of the current stream.
Note: It is possible to get a solution using a max-heap also instead of a min-heap, but would require some extra handling i.e. maintaining a max-heap of size (number_of_elements_at_time – K + 1) to get the Kth largest element.
Time Complexity:
O( log(K) * ( |initial_stream| + |append_stream| )).
To maintain a heap of size K = O(log(K)).
We push and pop every element of the initial and append stream at most once.
Auxiliary Space Used:
O(K).
Memory used to maintain a heap of size K = O(K).
Space Complexity:
O(|initial_stream| + |append_stream| + K).
Memory used for input = O(|initial_stream| + |append_stream|).
Memory used for output = O(|append_stream|).
Auxiliary space = O(K).
Total space complexity = O(|initial_stream| + |append_stream| + K).
// -------- START --------
vector kth_largest(int k, vector initial_stream, vector append_stream)
{
  // Priority queue's internal implementation is the same as a binary heap.
  priority_queue<int, vector, greater> min_heap;
  for(int i = 0; i < initial_stream.size(); i++) {     min_heap.push(initial_stream[i]);     // Make sure that the heap size does not exceed K.     if(min_heap.size() > k)
      min_heap.pop(); // Remove the element smaller than the K largest elements.
  }
  vector result;
  for(int i = 0; i < append_stream.size(); i++) {     min_heap.push(append_stream[i]);     // Make sure that the heap size does not exceed K.     if(min_heap.size() > k)
      min_heap.pop(); // Remove the element smaller than the K largest elements.
    // Adding current Kth largest element.
    result.push_back(min_heap.top());
  }
  return result;
}
// -------- END --------
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Just drop your name and email so we can send your Power Patterns PDF straight to your inbox. No Spam!
By sharing your contact details, you agree to our privacy policy.
Time Zone: Asia/Dhaka
We’ve sent the Power Patterns PDF to your inbox — it should arrive in the next 30 seconds.
📩 Can’t find it? Check your promotions or spam folder — and mark us as safe so you don’t miss future insights.
We’re hosting a private session where FAANG insiders walk through how they actually use these Power Patterns to crack interviews — and what sets top performers apart.
🎯 If you liked the PDF, you’ll love what we’re sharing next.
Time Zone: