Why the solution is showing TLE its N*Log(maximum element in the array)


#1

int Solution::kthsmallest(const vector &A, int k)
{
int maxm=A[0];
for(int i=1;i<A.size();i++)
{
maxm=max(maxm,A[i]);
}

 int i=0,j=maxm;
 
 while(i<j)
 {
     int mid=i+(j-i)/2;
     
     int cnt=0;
     for(int i=0;i<A.size();i++)
     {
         if(A[i]<=mid)
         cnt++;
     }
     if(cnt==k-1)
     {
         return mid;
     }
     else if(cnt<k)
     {
         i=mid+1;
     }
     else if(cnt>k)
     {
         j=mid-1;
     }
 }

}

Please help why this solution is showing TLE