
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find maximum of each k sized contiguous subarray
Suppose we have an array with n elements and a value k. We shall have to find maximum value for each of the contiguous subarray of size k.
So, if the input is like arr = [3,4,6,2,8], k = 3, then the output will be The contiguous subarrays of size 3 are [3,4,6], [4,6,2], [6,2,8], so the maximum elements are 6, 6 and 8.
To solve this, we will follow these steps −
- Define one deque Qi of size k
- for initialize i := 0, when i < k, update (increase i by 1), do:
- while Qi is not empty and arr[i] >= arr[last element of Qi], do:
- delete last element from Qi
- insert i at the end of Qi
- while Qi is not empty and arr[i] >= arr[last element of Qi], do:
- for i < size of arr, update (increase i by 1), do:
- display arr[first element of Qi]
- while Qi is not empty and first element of Qi <= i - k, do:
- delete front element from Qi
- while Qi is not empty and arr[i] >= arr[last element of Qi], do:
- delete last element from Qi
- insert i at the end of Qi
- display arr[first element of Qi]
Example
Let us see the following implementation to get better understanding −
#include <iostream> #include <vector> #include <deque> using namespace std; int main(){ vector<int> arr = {3,4,6,2,8}; int k = 3; 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 < arr.size(); ++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; }
Input
{3,4,6,2,8}, 3
Output
6 6 8
- Related Articles
- Program to find maximum product of contiguous subarray in Python
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Maximum contiguous sum of subarray in JavaScript
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
- Find maximum average subarray of k length in C++
- Program to find a list of numbers where each K-sized window has unique elements in Python
- Python program to find N-sized substrings with K distinct characters
- C/C++ Program for Largest Sum Contiguous Subarray?
- Program to find sum of contiguous sublist with maximum sum in Python
- Largest Sum Contiguous Subarray
- Program to find maximum ascending subarray sum using Python
- Program to find maximum subarray min-product in Python
- Find maximum (or minimum) sum of a subarray of size k in C++
- Program to find maximum absolute sum of any subarray in Python
- Program to find maximum score of a good subarray in Python

Advertisements