Min Max Equality

 Chef found an array 

of

elements. He defines a subarray as bad if the maximum element of the subarray is equal to the minimum element of the subarray.

More formally, a subarray

is bad if

.

Chef is allowed to change at most

elements of the array.
Find the minimum number of bad subarrays Chef can obtain after changing at most

elements of the array.

Input Format

  • First line will contain
  • , the number of test cases. Then the test cases follow.
  • Each test case contains two lines of input, the first line contains two space-separated integers
  • and
  • - the number of elements in the array and the maximum number of elements Chef can change respectively.
  • The second line contains
  • space-separated integers
    • - the initial array.

    Output Format

    For each test case, output the minimum number of bad subarrays Chef can obtain after changing at most

    elements of the array.

    Constraints

  • Sum of
  • over all test cases does not exceed
    • .

    Sample Input 1

    3
    3 1
    1 2 3
    4 2
    3 3 3 1
    5 1
    1 1 1 1 1
    

    Sample Output 1

    3
    4
    7
    

    Explanation

    Test case

    : The minimum number of bad subarrays Chef can obtain is . Chef does not need to perform any operations. The subarrays where the maximum element is equal to the minimum element are and where denotes the subarray starting at index and ending at index

    .

    Test case

    : The minimum number of bad subarrays Chef can obtain is . Chef can perform operations and change the array to . The subarrays where the maximum element is equal to the minimum element are and .
    Previous
    Next Post »