From 2f4ec54cd4db289386b298f71ead3f67f1a090fe Mon Sep 17 00:00:00 2001 From: Adarsh Kumar Singh <0902cs221005@rjit.ac.in> Date: Fri, 21 Mar 2025 22:48:58 +0530 Subject: [PATCH 1/4] Create Majority-element-ii.cpp Its solution and explaination for problem :https://leetcode.com/problems/majority-element-ii/description/ --- C++/Majority-element-ii.cpp | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 C++/Majority-element-ii.cpp 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 From 358402a6d2c2fe5ee22f317ef014eb683ad52686 Mon Sep 17 00:00:00 2001 From: Adarsh Kumar Singh <0902cs221005@rjit.ac.in> Date: Fri, 21 Mar 2025 23:18:31 +0530 Subject: [PATCH 2/4] Update README.md updating readme file --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 85accc7..f5a5227 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 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 | + +
@@ -518,6 +521,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [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/) From 2df2ae2902facc6fc4eb1f46d0e8ddfdb297c631 Mon Sep 17 00:00:00 2001 From: Adarsh Kumar Singh <0902cs221005@rjit.ac.in> Date: Fri, 21 Mar 2025 23:21:19 +0530 Subject: [PATCH 3/4] Update README.md temp --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f5a5227..94155ec 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 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 | +| 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 | |
From 6c65a457af63f39772230a6c033a764fc9411662 Mon Sep 17 00:00:00 2001 From: Adarsh Kumar Singh <0902cs221005@rjit.ac.in> Date: Fri, 21 Mar 2025 23:22:25 +0530 Subject: [PATCH 4/4] Update README.md update 2.0 --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 94155ec..4f8e98f 100644 --- a/README.md +++ b/README.md @@ -148,9 +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 | | +| 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 |
@@ -520,7 +518,6 @@ 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/)