Implementation of find the largest and the n/3 largest elements











up vote
3
down vote

favorite
1












Suppose you get numbers all the time and you don't know how many numbers will come at the beginning. At any time I want to be able to output the largest number and the n/3 largest number (where n is the number of numbers entered up to this point) immediately (in O(1)). The maximum time for entering new numbers is O(log(n)).



What is the best way to implement this ?










share|improve this question




















  • 1




    In essence this is a variant of a selection algorithm: cs.stackexchange.com/questions/1914/…
    – Willem Van Onsem
    Nov 9 at 20:46






  • 1




    Largest is easy. The n/3 largest I would handle with two heaps, a min heap and and a max heap. There are many variants but even the simple ones meet your requirements.
    – user3386109
    Nov 9 at 21:03

















up vote
3
down vote

favorite
1












Suppose you get numbers all the time and you don't know how many numbers will come at the beginning. At any time I want to be able to output the largest number and the n/3 largest number (where n is the number of numbers entered up to this point) immediately (in O(1)). The maximum time for entering new numbers is O(log(n)).



What is the best way to implement this ?










share|improve this question




















  • 1




    In essence this is a variant of a selection algorithm: cs.stackexchange.com/questions/1914/…
    – Willem Van Onsem
    Nov 9 at 20:46






  • 1




    Largest is easy. The n/3 largest I would handle with two heaps, a min heap and and a max heap. There are many variants but even the simple ones meet your requirements.
    – user3386109
    Nov 9 at 21:03















up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





Suppose you get numbers all the time and you don't know how many numbers will come at the beginning. At any time I want to be able to output the largest number and the n/3 largest number (where n is the number of numbers entered up to this point) immediately (in O(1)). The maximum time for entering new numbers is O(log(n)).



What is the best way to implement this ?










share|improve this question















Suppose you get numbers all the time and you don't know how many numbers will come at the beginning. At any time I want to be able to output the largest number and the n/3 largest number (where n is the number of numbers entered up to this point) immediately (in O(1)). The maximum time for entering new numbers is O(log(n)).



What is the best way to implement this ?







algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 20:57









bobbyrne01

2,09343791




2,09343791










asked Nov 9 at 20:35









azeug

212




212








  • 1




    In essence this is a variant of a selection algorithm: cs.stackexchange.com/questions/1914/…
    – Willem Van Onsem
    Nov 9 at 20:46






  • 1




    Largest is easy. The n/3 largest I would handle with two heaps, a min heap and and a max heap. There are many variants but even the simple ones meet your requirements.
    – user3386109
    Nov 9 at 21:03
















  • 1




    In essence this is a variant of a selection algorithm: cs.stackexchange.com/questions/1914/…
    – Willem Van Onsem
    Nov 9 at 20:46






  • 1




    Largest is easy. The n/3 largest I would handle with two heaps, a min heap and and a max heap. There are many variants but even the simple ones meet your requirements.
    – user3386109
    Nov 9 at 21:03










1




1




In essence this is a variant of a selection algorithm: cs.stackexchange.com/questions/1914/…
– Willem Van Onsem
Nov 9 at 20:46




In essence this is a variant of a selection algorithm: cs.stackexchange.com/questions/1914/…
– Willem Van Onsem
Nov 9 at 20:46




1




1




Largest is easy. The n/3 largest I would handle with two heaps, a min heap and and a max heap. There are many variants but even the simple ones meet your requirements.
– user3386109
Nov 9 at 21:03






Largest is easy. The n/3 largest I would handle with two heaps, a min heap and and a max heap. There are many variants but even the simple ones meet your requirements.
– user3386109
Nov 9 at 21:03














1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










You could implement it with two heaps: a min-heap (A), and a max-heap (B).



The idea is to store in A the n/3 greatest values, and the rest in B so that the maximum in B is the n/3 greatest value (counting zero-based). Assuming that n/3 is truncated down in case n is not a multiple of three, and that the resulting value is zero-based (n/3 == 0 means the maximum is also the n/3 greatest element), this means that the following invariance must be maintained:



A.size == floor((A.size + B.size)/3)


...which comes down to:



0 <= B.size - 2*A.size < 3 


The above condition might vary a bit depending on the interpretation what the n/3 element is (is it 0-based, 1-based, rounded, truncated,...), and some special care might be needed when there are fewer than 3 elements in the total collection, depending on that definition.



For adding a value v you would first decide in which heap it belongs. If it is smaller than the current n/3th greatest value -- the max value in B -- then it belongs in B, otherwise in A. After that addition the invariance might be broken and must be restored by pulling one value from either heap and adding it to the other.



More formally v is added as follows:



if v < B.peek():
B.add(v)
if B.size - 2*A.size >= 3:
A.add(B.pull())
else:
A.add(v)
if B.size - 2*A.size < 0:
B.add(A.pull())


The implementation for getting the n/3th greatest value would be:



B.peek()


The time complexity for the above used methods is:




  • size: O(1)

  • peek: O(1)

  • pull: O(logn)

  • add: O(logn)


To maintain the overall maximum is an easy task. The heaps only serve for keeping track of the n/3th greatest value.






share|improve this answer























  • Thank you very much!! But what do you mean with pull?
    – azeug
    Nov 9 at 23:06










  • Another name for pull is extract. It means taking the value from the root of the heap, and removing it from the heap, which then needs to get a new root via the heapify algorithm.
    – trincot
    Nov 9 at 23:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232931%2fimplementation-of-find-the-largest-and-the-n-3-largest-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










You could implement it with two heaps: a min-heap (A), and a max-heap (B).



The idea is to store in A the n/3 greatest values, and the rest in B so that the maximum in B is the n/3 greatest value (counting zero-based). Assuming that n/3 is truncated down in case n is not a multiple of three, and that the resulting value is zero-based (n/3 == 0 means the maximum is also the n/3 greatest element), this means that the following invariance must be maintained:



A.size == floor((A.size + B.size)/3)


...which comes down to:



0 <= B.size - 2*A.size < 3 


The above condition might vary a bit depending on the interpretation what the n/3 element is (is it 0-based, 1-based, rounded, truncated,...), and some special care might be needed when there are fewer than 3 elements in the total collection, depending on that definition.



For adding a value v you would first decide in which heap it belongs. If it is smaller than the current n/3th greatest value -- the max value in B -- then it belongs in B, otherwise in A. After that addition the invariance might be broken and must be restored by pulling one value from either heap and adding it to the other.



More formally v is added as follows:



if v < B.peek():
B.add(v)
if B.size - 2*A.size >= 3:
A.add(B.pull())
else:
A.add(v)
if B.size - 2*A.size < 0:
B.add(A.pull())


The implementation for getting the n/3th greatest value would be:



B.peek()


The time complexity for the above used methods is:




  • size: O(1)

  • peek: O(1)

  • pull: O(logn)

  • add: O(logn)


To maintain the overall maximum is an easy task. The heaps only serve for keeping track of the n/3th greatest value.






share|improve this answer























  • Thank you very much!! But what do you mean with pull?
    – azeug
    Nov 9 at 23:06










  • Another name for pull is extract. It means taking the value from the root of the heap, and removing it from the heap, which then needs to get a new root via the heapify algorithm.
    – trincot
    Nov 9 at 23:11















up vote
3
down vote



accepted










You could implement it with two heaps: a min-heap (A), and a max-heap (B).



The idea is to store in A the n/3 greatest values, and the rest in B so that the maximum in B is the n/3 greatest value (counting zero-based). Assuming that n/3 is truncated down in case n is not a multiple of three, and that the resulting value is zero-based (n/3 == 0 means the maximum is also the n/3 greatest element), this means that the following invariance must be maintained:



A.size == floor((A.size + B.size)/3)


...which comes down to:



0 <= B.size - 2*A.size < 3 


The above condition might vary a bit depending on the interpretation what the n/3 element is (is it 0-based, 1-based, rounded, truncated,...), and some special care might be needed when there are fewer than 3 elements in the total collection, depending on that definition.



For adding a value v you would first decide in which heap it belongs. If it is smaller than the current n/3th greatest value -- the max value in B -- then it belongs in B, otherwise in A. After that addition the invariance might be broken and must be restored by pulling one value from either heap and adding it to the other.



More formally v is added as follows:



if v < B.peek():
B.add(v)
if B.size - 2*A.size >= 3:
A.add(B.pull())
else:
A.add(v)
if B.size - 2*A.size < 0:
B.add(A.pull())


The implementation for getting the n/3th greatest value would be:



B.peek()


The time complexity for the above used methods is:




  • size: O(1)

  • peek: O(1)

  • pull: O(logn)

  • add: O(logn)


To maintain the overall maximum is an easy task. The heaps only serve for keeping track of the n/3th greatest value.






share|improve this answer























  • Thank you very much!! But what do you mean with pull?
    – azeug
    Nov 9 at 23:06










  • Another name for pull is extract. It means taking the value from the root of the heap, and removing it from the heap, which then needs to get a new root via the heapify algorithm.
    – trincot
    Nov 9 at 23:11













up vote
3
down vote



accepted







up vote
3
down vote



accepted






You could implement it with two heaps: a min-heap (A), and a max-heap (B).



The idea is to store in A the n/3 greatest values, and the rest in B so that the maximum in B is the n/3 greatest value (counting zero-based). Assuming that n/3 is truncated down in case n is not a multiple of three, and that the resulting value is zero-based (n/3 == 0 means the maximum is also the n/3 greatest element), this means that the following invariance must be maintained:



A.size == floor((A.size + B.size)/3)


...which comes down to:



0 <= B.size - 2*A.size < 3 


The above condition might vary a bit depending on the interpretation what the n/3 element is (is it 0-based, 1-based, rounded, truncated,...), and some special care might be needed when there are fewer than 3 elements in the total collection, depending on that definition.



For adding a value v you would first decide in which heap it belongs. If it is smaller than the current n/3th greatest value -- the max value in B -- then it belongs in B, otherwise in A. After that addition the invariance might be broken and must be restored by pulling one value from either heap and adding it to the other.



More formally v is added as follows:



if v < B.peek():
B.add(v)
if B.size - 2*A.size >= 3:
A.add(B.pull())
else:
A.add(v)
if B.size - 2*A.size < 0:
B.add(A.pull())


The implementation for getting the n/3th greatest value would be:



B.peek()


The time complexity for the above used methods is:




  • size: O(1)

  • peek: O(1)

  • pull: O(logn)

  • add: O(logn)


To maintain the overall maximum is an easy task. The heaps only serve for keeping track of the n/3th greatest value.






share|improve this answer














You could implement it with two heaps: a min-heap (A), and a max-heap (B).



The idea is to store in A the n/3 greatest values, and the rest in B so that the maximum in B is the n/3 greatest value (counting zero-based). Assuming that n/3 is truncated down in case n is not a multiple of three, and that the resulting value is zero-based (n/3 == 0 means the maximum is also the n/3 greatest element), this means that the following invariance must be maintained:



A.size == floor((A.size + B.size)/3)


...which comes down to:



0 <= B.size - 2*A.size < 3 


The above condition might vary a bit depending on the interpretation what the n/3 element is (is it 0-based, 1-based, rounded, truncated,...), and some special care might be needed when there are fewer than 3 elements in the total collection, depending on that definition.



For adding a value v you would first decide in which heap it belongs. If it is smaller than the current n/3th greatest value -- the max value in B -- then it belongs in B, otherwise in A. After that addition the invariance might be broken and must be restored by pulling one value from either heap and adding it to the other.



More formally v is added as follows:



if v < B.peek():
B.add(v)
if B.size - 2*A.size >= 3:
A.add(B.pull())
else:
A.add(v)
if B.size - 2*A.size < 0:
B.add(A.pull())


The implementation for getting the n/3th greatest value would be:



B.peek()


The time complexity for the above used methods is:




  • size: O(1)

  • peek: O(1)

  • pull: O(logn)

  • add: O(logn)


To maintain the overall maximum is an easy task. The heaps only serve for keeping track of the n/3th greatest value.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 22:33

























answered Nov 9 at 21:46









trincot

114k1477109




114k1477109












  • Thank you very much!! But what do you mean with pull?
    – azeug
    Nov 9 at 23:06










  • Another name for pull is extract. It means taking the value from the root of the heap, and removing it from the heap, which then needs to get a new root via the heapify algorithm.
    – trincot
    Nov 9 at 23:11


















  • Thank you very much!! But what do you mean with pull?
    – azeug
    Nov 9 at 23:06










  • Another name for pull is extract. It means taking the value from the root of the heap, and removing it from the heap, which then needs to get a new root via the heapify algorithm.
    – trincot
    Nov 9 at 23:11
















Thank you very much!! But what do you mean with pull?
– azeug
Nov 9 at 23:06




Thank you very much!! But what do you mean with pull?
– azeug
Nov 9 at 23:06












Another name for pull is extract. It means taking the value from the root of the heap, and removing it from the heap, which then needs to get a new root via the heapify algorithm.
– trincot
Nov 9 at 23:11




Another name for pull is extract. It means taking the value from the root of the heap, and removing it from the heap, which then needs to get a new root via the heapify algorithm.
– trincot
Nov 9 at 23:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232931%2fimplementation-of-find-the-largest-and-the-n-3-largest-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check