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 6975911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:25:46+00:00 2026-05-27T17:25:46+00:00

I have a stack of elements from which a random element has to be

  • 0

I have a stack of elements from which a random element has to be removed (i.e. all the elements which are between the top and that particular element will be popped and pushed again). And every time an element is popped, we have to determine how many times it has been popped earlier for other elements.

I have been on this since a long while. (The stack is dynamic (i.e. elements are being added and removed from time to time)).

  • 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-05-27T17:25:47+00:00Added an answer on May 27, 2026 at 5:25 pm

    I would store the stack as a singly linked list, and keep an integer in each node to represent the number of times it’s been accessed. IE, a stack with 5 on top, 7 on the bottom and no accesses made would look like:

    | 5 | -> | 2 | -> | 3 | -> | 1 | -> | 7 |
    | 0 | -> | 0 | -> | 0 | -> | 0 | -> | 0 |
    

    Then you could write your own pop (O(n)) which just iterates through the linked list adding 1 to access count for each node it visits (if you can assume that what you pop is always in the stack, then you only need to iterate through it once, if not you may need to iterate through twice) such that pop(3): // Returns 0

    | 5 | -> | 2 | -> | 1 | -> | 7 |
    | 1 | -> | 1 | -> | 0 | -> | 0 |
    

    pop(7): // Returns 0

    | 5 | -> | 2 | -> | 1 |
    | 2 | -> | 2 | -> | 1 |
    

    pop(2): // Returns 2

    | 5 | -> | 1 |
    | 3 | -> | 1 |
    

    push(6):

    | 6 | -> | 5 | -> | 1 |
    | 0 | -> | 3 | -> | 1 |
    

    pop(1): // Returns 1

    | 6 | -> | 5 |
    | 1 | -> | 4 |
    

    pop(6): // Returns 1

    | 5 |
    | 4 |
    

    pop(5): // Returns 4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of strings from which I need to remove all elements
I have two DIV-elements, which are assigned an array called stack using data() .
Because anything from C++ and beyond have nice constructs that handles a stack like
I have a stack panel inside of an expander panel that I programaticaly adds
I have a stack which contains some integer data. I want to find out
I'm outside gdb's target executable and I don't even have a stack that corresponds
I have read on Stack Overflow some people that have converting to C#2.0 to
I have the CakePHP stack in /var/www/site from one view under controller A I
I have a program which gets a list of locations from an sql db
I have an issue that has got me pretty stumped, and would appreciate some

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.