diff --git a/C++/Majority-element-ii.cpp b/C++/Majority-element-ii.cpp new file mode 100644 index 0000000..706a92d --- /dev/null +++ b/C++/Majority-element-ii.cpp @@ -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 majorityElement(vector& nums) { + sort(nums.begin(),nums.end()); + + int n=nums.size(); + int k=n/3; + vector res; + int count=0; + for(int i=0;ik) res.push_back(a); + } + return res; + + } + }; \ No newline at end of file diff --git a/README.md b/README.md index 85accc7..4f8e98f 100644 --- a/README.md +++ b/README.md @@ -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 |
@@ -517,7 +518,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) | [Amrit Kumar](https://github.com/amrit-GH23)
| India | C++ | [CodeChef](https://www.codechef.com/users/amrit_kumar08)
[Linkedin](https://www.linkedin.com/in/amrit-kumar-28053b253/) - +| [Adarsh Kumar Singh](https://github.com/Account1Adarsh)
| India | C++ | [CodeChef](https://www.codechef.com/users/rjit22cs05)
[Leet code](https://leetcode.com/u/the_explorer_adarsh/)
⬆️ Back to Top