I was writing this code for finding Kth smallest element in array where l= starting index and r = ending index is given to us as input parameter in function.
class Solution{
public:
int kthSmallest(int arr[], int l, int r, int k) {
//code here
arr.sort(l,l+r+1);
int count =1,i=1,var =0;
while(count<=k)
{
var = arr[i];
count++;
i++;
}
return var;
}
};
I tried to use sort function in my code but this code is giving error in my sort function ,given below which is not understood by me.
prog.cpp: In member function int Solution::kthSmallest(int*, int, int, int):
prog.cpp:18:13: error: request for member sort in arr, which is of non-class type int*
arr.sort(l,l+r+1);
^
arr[]is a C-style array. The C++ equivalent would bestd::array- infinitezero