Given an array of **N** elements, you are required to find the maximum sum of lengths of all non-overlapping subarrays with **K** as the maximum element in the subarray.

.

**Input:**

First line of the input contains an integer **T**, denoting the number of the total test cases. Then T test case follows. First line of the test case contains an integer **N**, denoting the number of elements in the array. Then next line contains N space separated integers denoting the elements of the array. The last line of each test case contains an integer **K**.

**Output:**

For each test case ouptut a single line denoting the sum of the length of all such subarrays.

**Constraints:**

1 <= T <= 100

1 <= N <= 10^{5}

1 <= A[] <= 10^{5}

**Example:
Input:**

3

9

2 1 4 9 2 3 8 3 4

4

7

1 2 3 2 3 4 1

4

10

4 5 7 1 2 9 8 4 3 1

4

5

7

4

{2, 1, 4} => Length = 3

{3, 4} => Length = 2

So, 3 + 2 = 5 is the answer

Test Case 2: {1, 2, 3, 2, 3, 4, 1} => Length = 7

**Test Case 3:**

{4} => Length = 1

{4, 3, 1} => Length = 3

So, 1 + 3 = 4 is the answer.

Author: harshitsidhwa

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

preparecodinginterview_dot_com | 190 |

__murugan__ | 173 |

manvimahajan20 | 140 |

xiaoyan_yunche | 139 |

ShameekAgarwal | 116 |

preparecodinginterview_dot_com | 190 |

__murugan__ | 173 |

manvimahajan20 | 166 |

SoumyaKaushik | 146 |

arpit_anshuman | 140 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.