 QUALPREL : Not understanding the reason for TLE,.

Why am I getting TLE when complexity is still O(nlogn) as mentioned in Editorial Solution and I used binary search in increasingly sorted array which is O(logn) instead of traversing elements in O(n)

#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int tests;
cin >> tests;
while(tests--){
int n, k;
cin >> n >> k;
int arr[n];
for(int i=0; i<n; i++){
cin >> arr[i];
}
sort(arr, arr+n);
int x = arr[n-(k-1)-1];
int low = 0, high = n-1, mid = (low+high)/2;
while(low<=mid){
if(arr[mid]<x)
low = mid+1;
else
high = mid-1;
mid = (low+high)/2;
}
cout << n-(high+1) << '\n';
}
return 0;
}

Try out the test case

1
6 6
1 2 3 3 3 4

In this test case your code will stuck in infinite loop because here according to your code
x will be 1
low will be 0 and high will be 5 and mid will be 2 in first pass
So arr  will be 3 which is greater than x so high becomes 1 and mid becomes 0
Now arr  will be 1 which is not less than x so high becomes -1 and mid becomes 0 here again arr  is not less than x so high becomes -1 and mmid becomes 0 and this will run forever like this and you won’t get your answer that’s why it’s giving TLE