Deque-STL in C++ HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Deque-STL in C++ HackerRank Solution. This problem is a part of the HackerRank C++ Programming Series.

Deque-STL in C++ HackerRank Solution
Deque-STL in C++ HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Deque-STL in C++ HackerRank Solution

Ezoicreport this adProblem

You inherited a piece of code that performs username validation for your company’s website. The existing function works reasonably well, but it throws an exception when the username is too short. Upon review, you realize that nobody ever defined the exception.

The inherited code is provided for you in the locked section of your editor. Complete the code so that, when an exception is thrown, it prints Too short: n (where n is the length of the given username).

Input Format :

The first line contains an integer, t, the number of test cases.
Each of the t subsequent lines describes a test case as a single username string, n.

Constraints :

  • 1 <= t <= 1000
  • 1<= |u| <= 100
  • The username consists only of uppercase and lowercase letters.

Output Format :

You are not responsible for directly printing anything to stdout. If your code is correct, the locked stub code in your editor will print either Valid (if the username is valid), Invalid (if the username is invalid), or Too short: n (where n is the length of the too-short username) on a new line for each test case.

Sample Input :

3
Peter
Me
Arxwwz

Sample Output :

Valid
Too short: 2
Invalid

Explanation :

Username Me is too short because it only contains 2 characters, so your exception prints Too Short : 2.
All other validation is handled by the locked code in your editor.

Deque-STL in C++ HackerRank Solution

#include <iostream>
#include <deque>
using namespace std;
void printKMax(int arr[], int n, int k)
{
	//Write your code here.
	/* Deque-STL in C++ - Hacker Rank Solution START */
    deque<int> Qi(k);
    int i;
    for (i = 0; i < k; i++)
    {
        while ((!Qi.empty()) && (arr[i] >= arr[Qi.back()]))
            Qi.pop_back();
        Qi.push_back(i);
    }
    for ( ; i < n; i++) {
        cout << arr[Qi.front()] << " ";
        while ((!Qi.empty()) && (Qi.front() <= i - k))
            Qi.pop_front();
        while ((!Qi.empty()) && (arr[i] >= arr[Qi.back()]))
            Qi.pop_back();
        Qi.push_back(i);
    }
    cout << arr[Qi.front()] << endl;
    /* Deque-STL in C++ - Hacker Rank Solution END */
}
int main()
{
	int t;
	cin >> t;
	while(t>0)
	{
		int n,k;
    	cin >> n >> k;
    	int i;
    	int arr[n];
    	for(i=0;i<n;i++)
      		cin >> arr[i];
    	printKMax(arr, n, k);
    	t--;
  	}
  	return 0;
}

Disclaimer: The above Problem (Deque-STL in C++) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Print Pretty in C++ HackerRank Solution

Sharing Is Caring

Leave a Comment