Deque-STL

  • + 2 comments

    I had a solution which pushed and pops, but that exceeded time limits. But the straight-forward solution below did as well. Anybody any clues?

    void printKMax(int arr[], int n, int k){
    	//Write your code here.
    	std::deque <int>que;
    	int cnt;
    	for (cnt=0 ; cnt<n ; cnt++) que.push_back(arr[cnt]); // just push them all
    	for (cnt=0;cnt<n-k;cnt++)
    		std::cout << *std::max_element(que.begin()+cnt,que.begin()+cnt+k) << ' ';
    	std::cout << *std::max_element(que.begin()+cnt,que.begin()+cnt+k) << std::endl;
    }