andreimargeloiu/UdemyCourse

Sort - might be wrong: the first element of the array is not taken into account)

vatavua opened this issue · 1 comments

https://github.com/margiki/UdemyCourse/blob/c7cb833d02728910eb1446c0c898938bf7895c9a/MajorityElement#L13

Are you sure the first argument of sort should be A+1? My opinion is that it should be A:
i.e.
std::sort(A,A+n+1);

The code is correct. Actually in the code we are ignoring the number present at zeroth(A[0]) index of the input array(Think of it as a dummy element) and starting our indexing from A[1] in order to apply the majority element finding algorithm,Same is for the case of sorting we are sorting only the elements starting from first index(A[1]) of the input array as we ignored A[0] index dummy element so we are applying the STL sort function with indexing (A+1,A+n+1).