From 5276967a127cf3bac4a24e52e390913edc29f666 Mon Sep 17 00:00:00 2001 From: danzang100 <12danysamuel@gmail.com> Date: Tue, 11 Mar 2025 19:11:37 +0530 Subject: [PATCH 1/2] Solution #31 - danzang100/Edited - 11.03.2025 - commit #1 --- .../Next Permutation - Solution.cpp | 20 +++++++++++++++++++ .../Next Permutation.md | 7 +++++++ 2 files changed, 27 insertions(+) create mode 100644 Arrays101/#31 - Next Permutation - Medium/Next Permutation - Solution.cpp create mode 100644 Arrays101/#31 - Next Permutation - Medium/Next Permutation.md diff --git a/Arrays101/#31 - Next Permutation - Medium/Next Permutation - Solution.cpp b/Arrays101/#31 - Next Permutation - Medium/Next Permutation - Solution.cpp new file mode 100644 index 0000000..78b4adc --- /dev/null +++ b/Arrays101/#31 - Next Permutation - Medium/Next Permutation - Solution.cpp @@ -0,0 +1,20 @@ +class Solution { + public: + void nextPermutation(vector& 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()); + } + }; \ No newline at end of file diff --git a/Arrays101/#31 - Next Permutation - Medium/Next Permutation.md b/Arrays101/#31 - Next Permutation - Medium/Next Permutation.md new file mode 100644 index 0000000..9999b8c --- /dev/null +++ b/Arrays101/#31 - Next Permutation - Medium/Next Permutation.md @@ -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. From ad2284ef524a372e7d4fcbb8ae6167b0fc084c5f Mon Sep 17 00:00:00 2001 From: danzang100 <12danysamuel@gmail.com> Date: Wed, 12 Mar 2025 23:02:22 +0530 Subject: [PATCH 2/2] Solution #169 - Daniel/Edited - 12.03.2025 --- .../Majority Element - Solution.cpp | 36 +++++++++++++++++++ .../Majority Element.md | 26 ++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 Arrays101/#169 - Majority Element - Easy/Majority Element - Solution.cpp create mode 100644 Arrays101/#169 - Majority Element - Easy/Majority Element.md diff --git a/Arrays101/#169 - Majority Element - Easy/Majority Element - Solution.cpp b/Arrays101/#169 - Majority Element - Easy/Majority Element - Solution.cpp new file mode 100644 index 0000000..33f8407 --- /dev/null +++ b/Arrays101/#169 - Majority Element - Easy/Majority Element - Solution.cpp @@ -0,0 +1,36 @@ +class Solution { + public: + int majorityElement(vector& nums) { + + //Sorting method + + // Hashmap method + unordered_map 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