Skip to main content

TCS

TCS OA-24 2024

Problem Description

You are given an array of integers, arr, and an integer k. Your task is to find the maximum element in each contiguous subarray of size k within arr.  

Input Format

  • The first line contains an integer N, representing the array's length.
  • The second line contains N space-separated integers.
  • The third line contains an integer k.

Output Format

Print an array containing the maximum element of each contiguous subarray of size k.

Constraints

  • 1 ≤ N ≤ 10⁴
  • -10³ ≤ arr[i] ≤ 10³
  • 1 ≤ k ≤ 10⁴

Example

Sample Input:

8
[1, 3, -1, -3, 5, 3, 6, 7]
3

Sample Output:

[3, 3, 5, 5, 6, 7]

Explanation:

The contiguous subarrays of size k=3 are:

  • [1, 3, -1] with maximum 3
  • [3, -1, -3] with maximum 3
  • [-1, -3, 5] with maximum 5
  • [-3, 5, 3] with maximum 5
  • [5, 3, 6] with maximum 6
  • [3, 6, 7] with maximum 7