Deque-STL in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Deque-STL in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Deque-STL in C++  - Hackerrank Solution

Problem

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++ – Hacker Rank 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;
}

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh