Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lack of Count Occurrences Algorithm #947

Open
ialimz opened this issue Sep 25, 2020 · 1 comment
Open

Lack of Count Occurrences Algorithm #947

ialimz opened this issue Sep 25, 2020 · 1 comment

Comments

@ialimz
Copy link

ialimz commented Sep 25, 2020

Brief Intro

Algorithm doesn't work if occurrences are in different indexes

More Details

for example consider using this array:
let a = [ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11, 3]

In this case, the algorithm returns 4 yet.
This is because finding difference between upper and lower bounds only works if occurrences pushed continuously

@KartikSatoskar
Copy link

KartikSatoskar commented Oct 21, 2020

@ialimz
To work this solution effectively our array should be sorted. Hence, we can apply binary search to find left/right boundaries of given element in sorted array. As per input mentioned above, binary search can't be applied(since array is not sorted).

To solve this problem of unsorted array we have following approaches:

  • Every time when new element gets added into array, blindly sort the array for safer side. But that would cost us extra O(NlogN) time complexity for every new element.

  • Find correct insertion position for new element. That would cost us O(logN + N) time complexity.

Hope this clarifies your doubt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants