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

Added problem number: 229 Majority Element II in C++. #503

Open
wants to merge 4 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
61 changes: 61 additions & 0 deletions C++/Majority-element-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*

Difficulty: Medium

_____________________________________________________________
Examples:
Example 1:

Input: nums = [3,2,3]
Output: [3]
Example 2:

Input: nums = [1]
Output: [1]
Example 3:

Input: nums = [1,2]
Output: [1,2]

______________________________________________________________________________________________________________________________

Steps and logic for below solution:

as vector nums contain integers and we have to find integers whose occurence is more than third size of vector nums.

we will sort the vector then traverse it and while traversing we will count for each elements occurnec
and if occrence is more than or equal to k(k=nums.size()/3) we will push that element in vector res;

return vector res;

time complexity is O(n+nlog(n))=>O(nlogn);


*/




class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
sort(nums.begin(),nums.end());

int n=nums.size();
int k=n/3;
vector<int> res;
int count=0;
for(int i=0;i<n;i++){
int a=nums[i];
count=0;
while(i<n and nums[i]==a){
i++;
count++;
}
i--;
if(count>k) res.push_back(a);
}
return res;

}
};
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array |
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array |
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [C++](./C++/238.Product_of_array_except_self) | O(N) | O(N) | Medium | Array |
| 229 | [Majority Element II](https://leetcode.com/problems/majority-element-ii/) | [C++](./C++/Majority-element-ii.cpp) | O(NlogN) | O(1) | Medium | Array, Sorting, Counting |


<br/>
Expand Down Expand Up @@ -517,7 +518,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
| [Shreyas Shrawage](https://github.com/shreyventure) <br> <img src = "https://avatars.githubusercontent.com/u/55741087?v=4" width="100" height="100"> | India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)<br/>[LeetCode](https://leetcode.com/shreyventure/)<br/>[HackerRank](https://www.hackerrank.com/shreyas_shrawage)
| [Surbhi Mayank](https://github.com/surbhi2408) <br> <img src="https://avatars.githubusercontent.com/u/58289829?s=400&u=68fd396819b927ec4d8820d87d6d1e311c3abd01&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/surbhi2408)
| [Amrit Kumar](https://github.com/amrit-GH23) <br> <img src="https://avatars.githubusercontent.com/u/146119347?s=400&u=599ba9f51c94e08861dc01580db03fadde609ad8&v=4" width="100" height="100"> | India | C++ | [CodeChef](https://www.codechef.com/users/amrit_kumar08)<br/>[Linkedin](https://www.linkedin.com/in/amrit-kumar-28053b253/)

| [Adarsh Kumar Singh](https://github.com/Account1Adarsh) <br> <img src="https://avatars.githubusercontent.com/u/142895773?s=400&v=4" width="100" height="100"> | India | C++ | [CodeChef](https://www.codechef.com/users/rjit22cs05)<br/>[Leet code](https://leetcode.com/u/the_explorer_adarsh/)
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
Expand Down