I have this function that is supposed to count how many duplicates of same number occur in certain array. Important this must be of complexity O(logn). I wrote this one below, but it doesn't count the duplicates properly. Also one more thing, the numbers are sorted from lowest to highest.
int CountElementsinFile(int *Arr, int num, int numOfD)
{
int avg{};
int inB = 0;
int inE = numOfD - 1;
int el{};
while (inB <= inE)
{
avg = (inB + inE) / 2;
if (Arr[avg] == num)
el++;
if (Arr[avg] > num)
inE = avg - 1;
else
inB = avg + 1;
}
return el;
}
numsubsequence using the dichotomy method. The difference of this values is subsequence length. Your implementation of the dichotomy method is incorrect.