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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
int majorityElement(vector<int>& nums) {

//Sorting method

// Hashmap method
unordered_map<int, int> umap;
for(int x: nums){
umap[x]++;
}
for(auto i: umap){
if(i.second >= ((double)nums.size()/2)){
return i.first;
}
}
return 0;

// Moore's voting method
int count = 0, candidate = nums[0];
for(int i=1; i<nums.size(); i++){
if(candidate == nums[i]){
count++;
}
else{
if(count == 0){
candidate = nums[i];
}
else{
count--;
}
}
}
return candidate;
}
};
26 changes: 26 additions & 0 deletions Arrays101/#169 - Majority Element - Easy/Majority Element.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Simple question of finding an element whose frequency is greater than half of the size of the array.

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.
Comment on lines +2 to +26
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i = nums.size()-1;
while(i > 0 && nums[i-1] >= nums[i]){
i--;
}

if(i==0){
reverse(nums.begin(), nums.end());
return;
}
int j = nums.size()-1;
while(j >= i && nums[j] <= nums[i-1]){
j--;
}
swap(nums[i-1], nums[j]);
reverse(nums.begin()+i, nums.end());
}
};
7 changes: 7 additions & 0 deletions Arrays101/#31 - Next Permutation - Medium/Next Permutation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.
Comment on lines +1 to +7
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!