-2

Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order.

Input: The first line of the input contains integer T denoting the number of test cases. For each test case, the first line takes an integer n denoting the size of array i.e number of nodes followed by n-space separated integers denoting the nodes of the tree in level order fashion.

Output: For each test case, the output is the level order sorted tree. Note: For every level, we only print distinct elements.

Input:

2
7
7 6 5 4 3 2 1
6
5 6 4 9 2 1

Output:

7
5 6
1 2 3 4
5
4 6
1 2 9

Code:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
    //code
    int t;
    cin>>t;
    while(t--)
    {priority_queue<int,vector<int>,greater<int>> m;
        int a;
        cin>>a;
        int k=1;
        int arr[a];
        queue<int> s;
        for(int i=0;i<a;i++)
        {
            cin>>arr[i];
            s.push(arr[i]);
            
        }
        while(s.empty()==false)
        {
            if(m.size()==k)
            {
                while(m.empty()==false)
                {
                    cout<<m.top()<<" ";
                }
                k=k*2;
                   cout<<endl;
            }
         else
             {
            
               m.push(s.front());
               s.pop();
             }
            
        }
        if(m.empty()==false)
        {
            while(m.empty()==false)
            {
                cout<<m.top()<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}

ERROR:OUTPUT LIMIT EXCEEDED!!!

Evg
  • 25,259
  • 5
  • 41
  • 83
  • [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Aug 30 '20 at 06:41

1 Answers1

1

This code

          while(m.empty()==false)
            {
                cout<<m.top()<<" ";
            }

is an infinite loop. Maybe you meant something like this (just guessing)

          while (!m.empty())
          {
              cout << m.top() << " ";
              m.pop();
          }
john
  • 85,011
  • 4
  • 57
  • 81