Question

Find the kth-largest number in an array of size more than 1,000,000 in O(N).

Find the kth-largest number in an array of size more than 1,000,000 in O(N).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
1. Take a Min Heap of size K.
2. One by one iterate on each element of array. There can be various scenarios as below:
        i) If the heap size is less than k, then insert the element to heap.
        ii) Else, If the element is less than the root node of heap, then ignore it.
        ii) Else, Remove an element from heap, and then add current element to the heap.
3. After pushing k largest element of the array to the heap, We need to get the maximum element from the heap.
4. To get max element of heap, Remove k elements one by one and the last removed element will be our result.

This algorithm is an O(N) algorithm, because K is considered very small compared to N, As N is array size.. So Insertion and deletion from the heap are considered to be O(1), and as the algorithm checks N element, It becomes an O(N) algorithm.
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Find the kth-largest number in an array of size more than 1,000,000 in O(N).
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a...

    a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>>              implements MinHeapInterface<T> {    private T[] heap;      // Array of heap entries; ignore heap[0]    private int...

  • Provide an O(k log k) algorithm that uses a heap data structure to find the kth...

    Provide an O(k log k) algorithm that uses a heap data structure to find the kth largest element from the heap of n elements where n > k. Sketch your algorithm. (Hint: You may need to use an additional heap) Use the example data below to demonstrate the process. (e.g. Find the 7 largest element from this heap and it should be 45) Justify the running time. Heap H 100 80 70 40 50 65 60 20 40 10 30...

  • in java Write a program that uses recursion to find the largest number in an array....

    in java Write a program that uses recursion to find the largest number in an array. Declare and initialize an array of 10 different numbers.

  • Write a program in C++ language that finds the largest number in an array of positive...

    Write a program in C++ language that finds the largest number in an array of positive numbers using recursion. Your program should have a function called array_max that should pass in an int array, and an int for the size of the array, respectively, and should return an int value. As an example, given an array of size 10 with contents: {10, 9, 6, 1, 2, 4, 16, 8, 7, 5} The output should be: The largest number in the...

  • Hi, I am having trouble with the following question: Given an unsorted array with integers, find...

    Hi, I am having trouble with the following question: Given an unsorted array with integers, find the median of it using the Quick Select method we’ve discussed in the class (Hint: We use the quick select to find the kth smallest element in an unsorted array in O(n) time complexity). Note: A median is the middle number of the array after it is sorted. And in this problem, we return the N/2-th number after sorted if there are even numbers...

  • please write this code in c++ // a: an array of ints. size is how many...

    please write this code in c++ // a: an array of ints. size is how many ints in array 7/ Return the largest value in the list. 7/ This value may be unique, or may occur more than once // You may assume size >= 1 int largestValue(int *a, int size) { assert(size >= 1); return -42; // STUB !!! Remove and replace with correct code 71 a: an array of ints. size is how many ints in array 7/...

  • Assume that we start with a random array of size n= 2k-1 and form a heap....

    Assume that we start with a random array of size n= 2k-1 and form a heap. a) What is the probability that the third largest element will be a child of the root? Justify b) Before you form a heap, you notice that none of the three smallest elements are near the top of the array, or more formally none of them are in any of the first (n-3)/4 locations of the array. What is the probability that the third...

  • Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers)...

    Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...

  • Modify the algorithm to solve the problem of finding the k-th largest number in array A

    Modify the algorithm to solve the problem of finding the k-th largest number in array A, 1≤k≤n, without sorting the entire array. Partsof the algorithm are given below. Fill in the blanks.                 Select-k-th-largest(A: Array [1..n] of numbers)                 1               for _____________________                 2                                 ________________                 3                                 for _____________________                 4                                                   if _______________ then ___________                 5                                 if position ≠ i then                 6                                                   temp=A[i]                 7                                                   A[i]=A[position]                 8                                                   A[position]=temp

  • Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a...

    Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a Divide & Conquer algorithm to return the number of items with values less than a given value (x). (5 points) 2. Test your algorithm by generating an array A of size n = 1024 random floating-point numbers with each number R between 0 and 1, i.e. 0.0 <R< 1.0. Run the algorithm to find the percentage of the numbers in the array with values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT