Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

How can I access and erase every 2nd last element from a multiset?

Revision en2, by Night_Lord, 2020-11-08 11:53:01

I was lately trying to access every 2nd last element from a multiset container. Here I want to first print every 2nd last element from the container then erase it from the container. This process continues until the size of the container reaches to 1.

My so far approach for this process is given bellow-

    int n;cin>>n;
    multiset<int>st;
    for(int i=0;i<n;++i)
    {
        int x;cin>>x;st.insert(x);
    }
    while(st.size()!=1)
    {
        auto it=st.rbegin();
        prev(it,1);
        cout<<*it<<" ";
        st.erase(*it);
    } 

Here, my expected results for the given case is-

6
0 0 1 0 1 1

ans- 1 1 0 0 0

Thanks in advance.

Tags #c++, #multiset

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Night_Lord 2020-11-08 11:53:01 24 Tiny change: ' \n~~~~~\nint n;cin>' -> ' \n~~~~~\n int n;cin>'
en1 English Night_Lord 2020-10-13 09:26:33 754 Initial revision (published)