Showing:
Handle Score
@Ibrahim Nash 6420
@mb1973 5704
@Quandray 5245
@akhayrutdinov 5111
@saiujwal13083 5046
@sanjay05 3762
@kirtidee18 3673
@mantu_singh 3530
@marius_valentin_dragoi 3523
@sushant_a 3459
Number of subarrays with maximum values in given range
Medium Accuracy: 46.18% Submissions: 325 Points: 4

Given an array of N elements and L and R, print the number of sub-arrays such that the value of the maximum array element in that subarray is at least L and at most R.

Example 1:

Input : Arr[] = {2, 0, 11, 3, 0}
L = 1 and R = 10
Output : 4
Explanation:
The sub-arrays {2}, {2, 0}, {3} and {3, 0}
have maximum in range 1-10.



Example 2:

Input : Arr[] = {3, 4, 1}
L = 2 and R = 4
Output : 5



This is a function problem. The input is already taken care of by the driver code. You only need to complete the function countSubarrays() that takes an array (arr), sizeOfArray (n), element L, integer R, and return the number of subarray with the maximum in range L-R. The driver code takes care of the printing.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ N ≤ 105
1 ≤ L ≤ R ≤ 106