Skip to content

Commit

Permalink
solution(java): Problems 229 and 287
Browse files Browse the repository at this point in the history
  • Loading branch information
godkingjay authored Oct 8, 2023
2 parents 347852c + 4b5c058 commit 25ad406
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Medium/229.MajorityElement-ll/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
The logic behind this code is to find the majority elements in an input array `nums`. A majority element is defined as an element that appears more than `n/3` times in the array, where `n` is the length of the array. The code achieves this by iterating through the sorted array and keeping track of the count of each element.

Here's a step-by-step explanation of the logic:

## 1. Initial Setup:
- An empty `ArrayList` called `result` is created to store the majority elements found.
- If the length of the input array `nums` is less than 3, it's a special case because in small arrays, any element that appears once or twice is considered a majority element. In this case, the code simply adds unique elements to the `result` list (ensuring no duplicates) and returns it.

## 2. Sorting:
- If the input array has 3 or more elements, it sorts the array in ascending order using `Arrays.sort(nums)`. Sorting the array simplifies the process of counting the occurrences of each element because identical elements will be adjacent to each other after sorting.

## 3. Counting Elements:
- The code then iterates through the sorted array using a loop.
- It maintains a `count` variable initialized to 1 to keep track of how many times the current element appears.
- If the current element is the same as the next element (i.e., nums[i] == nums[i+1]), it increments the `count` because it has encountered another occurrence of the same element.
- If the count is greater than 1 (meaning there is a repeated element) and the current element is not the same as the next element, it means that the current element has ended its consecutive appearances. In this case, the `count` is reset to 1.

## 4. Checking for Majority Elements:
- After counting the occurrences of each element in the sorted array, the code checks if the `count` of the current element is greater than or equal to `gt + 1`, where `gt` is calculated as `nums.length / 3`. If this condition is met, it means that the current element appears more than `n/3` times in the original unsorted array.
- When this condition is satisfied, the current element is added to the `result` list (if it's not already in the list to avoid duplicates), and the `count` is reset to 1.

## 5. Final Result:
- Once the loop has iterated through the entire sorted array, the `result` list contains the majority elements that satisfy the condition of appearing more than `n/3` times in the input array.

34 changes: 34 additions & 0 deletions Medium/229.MajorityElement-ll/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include <map>

using namespace std;

class Solution
{
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> vec;
int n= floor(nums.size()/3)+1;
map<int,int> mp;
map<int,int>::iterator it;
for(int i=0;i<nums.size();i++){
it=mp.find(nums[i]);
if(it==mp.end())
mp.insert(pair<int,int>(nums[i],1));
else it->second=mp[nums[i]]+1;
}
for(it=mp.begin();it!=mp.end();it++){
if(it->second>=n)
vec.push_back(it->first);
}
return vec;
}
};

int main()
{

// call the fn here
return 0;
}
32 changes: 32 additions & 0 deletions Medium/229.MajorityElement-ll/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution{
public List<Integer> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<>();
if(nums.length < 3){
int n = nums.length;
for(int i=0; i<n; i++){
if(!result.contains(nums[i]))
result.add(nums[i]);
}
return result;
}

Arrays.sort(nums);
int count=1;
int gt = nums.length/3;
for(int i=0; i<nums.length-1; i++){
if(nums[i] == nums[i+1]){
count++;
}
if(count > 1 && nums[i] != nums[i+1]){
count = 1;
}
if(count >= gt+1){
if(!result.contains(nums[i])){
result.add(nums[i]);
count = 1;
}
}
}
return result;
}
}

0 comments on commit 25ad406

Please sign in to comment.