Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Deque-STL in C++ – Solution in Hacker Rank - hackerranksolutions8

 

Problem

Double ended queue or Deque(part of C++ STL) are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). The member functions of deque that are mainly used are:

Deque Template:

std::deque<value_type>


Declaration:

deque<int> mydeque; //Creates a double ended queue of deque of int type


Size :

int length = mydeque.size(); //Gives the size of the deque


Push:

mydeque.push_back(1); //Pushes element at the end
mydeque.push_front(2); //Pushes element at the beginning


Pop:

mydeque.pop_back(); //Pops element from the end
mydeque.pop_front(); //Pops element from the beginning


Empty:

mydeque.empty() //Returns a boolean value which tells whether the deque is empty or not

To know more about deque, click here

Given a set of arrays of size N and an integer K, you have to find the maximum integer for each and every contiguous subarray of size K for each of the given arrays.


Input Format :

First line of input will contain the number of test cases T. For each test case, you will be given the size of array N and the size of subarray to be used K. This will be followed by the elements of the array Ai.

Constraints :

  • 1 <= N <= 1000
  • 1 <= T <= 10000
  • 1 <= K <= N
  • 1 <= Ai <= 10000, where Ai, is the is the ith element in the Array A;

Output Format :

For each of the contiguous subarrays of size K of each array, you have to print the maximum integer.


Sample Input :

2
5 2
3 4 6 3 4
7 4
3 4 5 8 1 4 10

Sample Output :

4 6 6 4
8 8 8 10

Explanation :

For the first case, the contiguous subarrays of size 2 are {3,4},{4,6},{6,3} and {3,4}. The 4 maximum elements of subarray of size 2 are: 4 6 6 4.

For the second case,the contiguous subarrays of size 4 are {3,4,5,8},{4,5,8,1},{5,8,1,4} and {8,1,4,10}. The 4 maximum element of subarray of size 4 are: 8 8 8 10.


Solution :

//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;
}

Comments