You are given two integers N, K, and an array X of N elements.
Write a program to find the sum of the lengths of the sub-arrays with K as its maximum number.
Any two sub-arrays that are considered should not overlap each other.
Find the maximum possible sum.
Note:
Example
Assumption
Approach
There is no K=4 in the array X.
Therefore, the answer is 0.
Function description
Complete the solve function provided in the editor. This function takes the following three parameters and returns the answer.
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).
Output format
For each test case, print the answer in a new line.
Constraints
1≤T≤50
1≤N≤3×105
1≤X[i],K≤108 (X[i] denotes the ith element of the array X)
Sum of N over all test cases in a file will be at most 5×106
Code snippets (also called starter code/boilerplate code)
This question has code snippets for C, CPP, Java, and Python.
The first line represents T=2 test cases.
Testcase 1:
Given
Approach
The indexes of subarray which have 4 as the maximum number are:
You cannot pick overlapping arrays. Therefore, you will only pick subarray 1 and subarray 3.
Hence, the answer is 1+2 = 3
Testcase 2:
It is explained in the Example above.