Count the number of subarrays whose maximum element is K.

Given an array arr of size N and an integer K. Your task is to find the number of subarrays in arr such that the maximum element in each subarray is exactly K.

Input: N = 5 , K = 3, arr = [1, 3, 2, 3, 1] Output: 4

Approach:
Use sliding window to solve the problem for each subarray find the maximum value and check whether the max is greater than k or not.If it is greater than k simply break the inner loop if the max is equal to simply increase the counter variable.


Feel free to connect with me:

Comments