-
Notifications
You must be signed in to change notification settings - Fork 20
Solution #31 - Daniel/Edited - 11.03.2025 #25
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
base: master
Are you sure you want to change the base?
Conversation
2 problems to be added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow the instructions that were shared with the other PR
|
||
Three possible methods: | ||
|
||
1. Sorting: | ||
|
||
- Time Complexity: O(nlogn) | ||
- Space Complexity: O(1) | ||
|
||
Simply sort the array and the middle element should always be the majority element. | ||
|
||
2. Hashmap: | ||
|
||
- Time Complexity: O(n) | ||
- Space Complexity: O(n) | ||
|
||
Map the frequencies of the elements to a hashmap and return the element with the highest frequency. | ||
|
||
3. Moore's Voting Algorithm: | ||
|
||
- Time Complexity: O(n) | ||
- Space Complexity: O(1) | ||
|
||
Initialise count and candidate element. If the element is the candidate element, increment count. Else if count is 0, change the candidate element to be the current element, else decrement the count. | ||
|
||
Works on the principle that upon complete iteration of the array, the majority element should always have the highest count and therefore end up being the candidate element all the time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly add the corresponding solutions as well, and also explain the time and space complexity + Try to give a brief on the various algorithms (+ Link articles) on your approach to this.
To solve this question, | ||
|
||
1. We find the first decreasing element from the right calling it the pivot. | ||
2. We check if that element does not exist, which means that the array is sorted in non-ascending order, hence we just return a reversed array. | ||
3. If the element exists, we find the smallest element greater than the pivot element which is to the right of it. | ||
4. We swap the two elements. | ||
5. We reverse the suffix of the pivot element to obtain the answer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to maintain consistency with the approach to explanations. Can be much better!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to maintain consistency with how you break down and explain problems.
No description provided.