#include <iostream>
using namespace std;

class minheap
{
   vector<int> heap;
   public:
    minheap()
    {
        heap.push_back(-1);
    }
    bool isEmpty()
    {
       if(heap.size() == 1) return true;
       else
       return false;
    }

    int top()
    {
      if(isEmpty())
        return -1;

      return heap[1];
    }

    int pop()
    {
        if(isEmpty())
        {
            return -1;
        }
        int top_ele =  heap[1];
        swap(heap[1],heap.back());
        heap.pop_back();
        int n = heap.size();
        int i=1;

       while(i<n)
       {
        if(i*2>=n) return top_ele;
        if(i*2 + 1>=n)
        {
            int left = heap[2*i];
            if(heap[i]>left)
            {
                swap(heap[i],heap[2*i]);
            }

            return top_ele;
        }
        else
        {
            int left = heap[2*i];
            int right= heap[2*i + 1];
          
            if(left<=right && heap[i]>=left)
            {
                 swap(heap[i],heap[2*i]);
                 i=i*2;
            }
            else if(right<=left && heap[i]>=right)
            {
                 swap(heap[i],heap[2*i+1]);
                 i=i*2+1;
            }
            else
            {
                return top_ele;
            }    
        }
       }
       return top_ele;
    }

    void push(int data)
    {
        heap.push_back(data);
        
        int i = heap.size()-1;
         
         while(i>1 && heap[i]<=heap[i/2])
         {
            swap(heap[i],heap[i/2]);
            i=i/2;
         }  

         cout<<heap[i]<<endl;
    }
};

class Solution {
public:
    vector<int> numberGame(vector<int>& nums) {
        
        minheap m;
        vector<int> vec;
        for(auto data : nums)
        {
            m.push(data);
        }

        while(!m.isEmpty())
        {
            int a = m.pop();
            int b = m.pop();
            vec.push_back(b);
            vec.push_back(a);
        }

        return vec;

    }
};

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: