Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

danzang100
Copy link

No description provided.

@danzang100 danzang100 closed this Mar 12, 2025
@danzang100
Copy link
Author

2 problems to be added.

@danzang100 danzang100 reopened this Mar 12, 2025
@danzang100 danzang100 changed the title Solution #31 - danzang100/Edited - 11.03.2025 - commit #1 Solution #31 - danzang100/Edited - 11.03.2025 Mar 12, 2025
@JRS296 JRS296 self-requested a review March 17, 2025 12:44
Copy link
Member

@JRS296 JRS296 left a 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

@danzang100 danzang100 changed the title Solution #31 - danzang100/Edited - 11.03.2025 Solution #31 - Daniel/Edited - 11.03.2025 Mar 18, 2025
Comment on lines +2 to +26

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.
Copy link
Member

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.

Comment on lines +1 to +7
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.
Copy link
Member

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!

Copy link
Member

@JRS296 JRS296 left a 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.

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

Successfully merging this pull request may close these issues.

2 participants