Deque-STL

  • + 0 comments

    You need to find the max of the K values. Then while you do not will pop the max value, and not arrive the end of queue, continue comparing to the next value, printing the max and pop_front of queue. If the value to pop is the max value, then start again and find the max of the K values.

    This is not optimized and if a lot of max values are repeated the complexity will increment. But this code pass all the tests in time.

    void printKMax(int arr[], int n, int k){
    	//Write your code here.
        deque<int> fila;
        deque<int>::iterator itr;
        int max = 0;
        
        for (int i = 0; i<n; i++)
        {
            fila.push_back(arr[i]);
        }
        
        while(fila.size() >= k)
        {
            itr = fila.begin();
            for(int i = 0; i<k ; i++)
            {
                if(*itr > max)
                    max = *itr;
                itr++;
            }
            cout << max << " ";
            while ((fila.front() != max) && (itr != fila.end()))
            {
                if (max < *itr)
                    max = *itr;
                cout << max << " ";
                itr++;
                fila.pop_front();
            }
            fila.pop_front();
            max = 0;
        }
        cout << endl;
    }