Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8259109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:46:58+00:00 2026-06-08T02:46:58+00:00

I have fifo query where I put and eject doubles. After each update I

  • 0

I have fifo query where I put and eject doubles.
After each update I need max and min values. I don’t need position (or index) of these values in query.
How to do that effectively? log(N) or probably even O(1)?

upd: found this Implement a queue in which push_rear(), pop_front() and get_min() are all constant time operations

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:46:58+00:00Added an answer on June 8, 2026 at 2:46 am

    This is a tricky question. Consider the following:

    Say the size of your fifo at any given time is N.
    Say that you track the min and max with just a pair of floats.
    Say that the size of the fifo remains reasonably constant.

    We can therefore assume that one “operation” on the queue logically consists of one push and one pop.

    Say you are comparing 2 methods of handling this: one uses a heap pair and one that uses a naive compare and search.

    For the heap method:

    Each operation, you push to the list and both heaps, then pop from the list and both heaps. The heap operations are always O(log(n)) and the list operation is O(1), so as N is large, the time complexity of one operation is O(log(N)) average case. It’s important to note that the heap operations are always this complexity regardless of whether or not the currently popped element is a min or max element. Thus, N operations has a time complexity of O(N*log(N)).

    For the naive method:

    Each operation, you push and pop the list, and compare the popped item to the stored min and max. If the item is the same as either one, you search the list either for an item of equal value (in which case you break early) or otherwise through the entire rest of the list, until you find the next best element. You then update the min/max with the next best. This method has O(1) typical case and O(N) worst case (min or max needs an update). It’s important to note that for some range of N numbers the number of times you will need to update min and max goes to a constant, and the number of times you won’t goes to N. Therefore, N operations has a time complexity of O(N). The naive case is actually better than a more advanced solution.

    That said, I don’t think heaps can efficiently remove elements, so you’d run into lots of trouble that way.

    Thus, consider the following pseudocode:

    queue fifo;
    float min, max;
    
    void push(float f)
    {
        if (fifo.size == 0)
        {
            min = f;
            max = f;
        }
        else if (f > max) max = f;
        else if (f < min) min = f;
    
        fifo.push(f);
    }
    
    float pop()
    {
        if (fifo.size == 0) return (*((float *)NULL)); // explode
        float f = fifo.pop();
        if (fifo.size == 0)
        {
            min = NaN;
            max = NaN;
            return f;
        }
        if (f == max) search_max(fifo);
        if (f == min) search_min(fifo);
        return f;
    }
    
    search_max(queue q)
    {
        float f = min;
        for (element in q)
        {
            if (element == max) return;
            if (element > f) f = element;
        }
        max = f;
    }
    
    search_min(queue q)
    {
        float f = max;
        for (element in q)
        {
            if (element == min) return;
            if (element < f) f = element;
        }
        min = f;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use a FIFO structure in my application. It needs to have
I need to create a fifo function for price calculation. I have a table
I have this FIFO which I'm going to use to store data from network
I have two programs, Writer and Reader. I have a FIFO from Writer to
I have a website that allows users to query for specific recipes using various
The SimpleThreadPool class shipped along with Quartz Scheduler does not have a FIFO behavior.
While implementing a FIFO I have used the following structure: struct Node { T
If I have many queues and each has an unique ID, would a Hashtable
I have a situation where I need to check if the other side of
I created a new FIFO using the mkfifo command. I have a text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.