Skip to content

Commit b910eab

Browse files
author
Sanajit Jana
committed
Add question and modify question count action
1 parent adfbc47 commit b910eab

File tree

4 files changed

+154
-13
lines changed

4 files changed

+154
-13
lines changed

.github/scripts/update_readme.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
README_PATH = "README.md"
77

8-
# Count problems (only .java files, recursively)
8+
# Count problems (count problem directories under p folder)
99
def count_problems():
1010
count = 0
11-
for folder in ["Easy", "Medium", "Hard"]:
12-
if os.path.exists(folder):
13-
for root, _, files in os.walk(folder):
14-
for file in files:
15-
if file.endswith(".java"):
16-
count += 1
11+
folder = "p"
12+
if os.path.exists(folder):
13+
for item in os.listdir(folder):
14+
item_path = os.path.join(folder, item)
15+
if os.path.isdir(item_path) and not item.startswith('.'):
16+
count += 1
1717
return count
1818

1919
# Get last commit date of the repo

.github/workflows/update-readme.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ jobs:
2121
with:
2222
python-version: "3.x"
2323

24-
- name: Debug - List files
24+
- name: Debug - List directories
2525
run: |
26-
echo "Listing .java files in Easy/Medium/Hard:"
27-
find Easy Medium Hard -type f -name '*.java' 2>/dev/null | sort || true
26+
echo "Listing problem directories in p folder:"
27+
find p -type d -mindepth 1 -maxdepth 1 2>/dev/null | sort || true
2828
echo "Total:"
29-
find Easy Medium Hard -type f -name '*.java' 2>/dev/null | wc -l || true
29+
find p -type d -mindepth 1 -maxdepth 1 2>/dev/null | wc -l || true
3030
3131
- name: Run update script
3232
run: python .github/scripts/update_readme.py

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ This repository is licensed under the [MIT License](LICENSE).
6363

6464
## 🏆 Progress
6565

66-
- Problems solved: 26
67-
- Last updated: 10 Oct 2025, 05:59 PM UTC+05:30
66+
- Problems solved: 19
67+
- Last updated: 11 Oct 2025, 10:20 AM UTC+05:30

p/0169-majority-element/README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# [Majority Element](https://leetcode.com/problems/majority-element/)
2+
3+
## Question Description
4+
Given an array `nums` of size `n`, return the majority element. The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
5+
6+
---
7+
8+
## Constraints
9+
- `1 <= nums.length <= 5 * 10^4`
10+
- `-10^9 <= nums[i] <= 10^9`
11+
12+
---
13+
14+
## Approach 1: Brute Force (Nested Loops)
15+
16+
Use nested loops to count occurrences of each element. For each element, scan the entire array to count how many times it appears. If any element appears more than n/2 times, return it.
17+
18+
**Time Complexity:** O(n²) - for each element, we scan the entire array
19+
**Space Complexity:** O(1) - no extra data structures used
20+
21+
```java
22+
class Solution {
23+
public int majorityElement(int[] nums) {
24+
for (int i = 0; i < nums.length; i++) {
25+
int count = 0;
26+
for (int j = 0; j < nums.length; j++) {
27+
if (nums[i] == nums[j]) count++;
28+
}
29+
if (count > nums.length / 2) return nums[i];
30+
}
31+
return -1;
32+
}
33+
}
34+
```
35+
36+
---
37+
38+
## Approach 2: Sorting
39+
40+
Sort the array and return the middle element. Since the majority element appears more than n/2 times, it will always occupy the middle position after sorting.
41+
42+
**Time Complexity:** O(n log n) - due to sorting
43+
**Space Complexity:** O(1) or O(log n) depending on sorting implementation
44+
45+
```java
46+
class Solution {
47+
public int majorityElement(int[] nums) {
48+
Arrays.sort(nums);
49+
return nums[nums.length / 2];
50+
}
51+
}
52+
```
53+
54+
---
55+
56+
## Approach 3: HashMap Counting
57+
58+
Use a HashMap to count occurrences of each element. Iterate through the array once to build the frequency map, then find the element with count greater than n/2.
59+
60+
**Time Complexity:** O(n) - single pass to count frequencies
61+
**Space Complexity:** O(n) - hashmap stores up to n elements
62+
63+
```java
64+
class Solution {
65+
public int majorityElement(int[] nums) {
66+
HashMap<Integer, Integer> map = new HashMap<>();
67+
for (int ele : nums) {
68+
map.put(ele, map.getOrDefault(ele, 0) + 1);
69+
}
70+
int level = nums.length / 2;
71+
for (int key : map.keySet()) {
72+
if (map.get(key) > level) return key;
73+
}
74+
return -1;
75+
}
76+
}
77+
```
78+
79+
---
80+
81+
## Approach 4: Boyer-Moore Voting Algorithm (Optimal)
82+
83+
Use a voting algorithm that maintains a candidate element and a count. This algorithm leverages the fact that the majority element appears more than n/2 times:
84+
- If count is 0, set current element as new candidate
85+
- If current element matches candidate, increment count
86+
- If current element differs from candidate, decrement count
87+
88+
The majority element will always be the final candidate.
89+
90+
**Time Complexity:** O(n) - single pass through the array
91+
**Space Complexity:** O(1) - only using constant extra space
92+
93+
```java
94+
class Solution {
95+
public int majorityElement(int[] nums) {
96+
int candidate = 0, count = 0;
97+
for (int num : nums) {
98+
if (count == 0) {
99+
candidate = num;
100+
}
101+
if (num == candidate) {
102+
count++;
103+
} else {
104+
count--;
105+
}
106+
}
107+
return candidate;
108+
}
109+
}
110+
```
111+
112+
---
113+
114+
## Dry Run Example
115+
Example Input: `nums = [3,2,3]`
116+
117+
**Approach 1 (Brute Force):**
118+
- i=0, nums[0]=3, inner loop finds 2 occurrences of 3, 2 > 1, return 3
119+
- i=1, nums[1]=2, inner loop finds 1 occurrence of 2, 1 not > 1
120+
121+
**Approach 2 (Sorting):**
122+
- Sorted array: [2,3,3], middle element is 3
123+
124+
**Approach 3 (HashMap):**
125+
- Map: {3:2, 2:1}, 3 appears 2 times > 1, return 3
126+
127+
**Approach 4 (Voting):**
128+
- i=0, num=3, count=0, set candidate=3, count=1
129+
- i=1, num=2, count=1, 2 ≠ 3, count=0
130+
- i=2, num=3, count=0, set candidate=3, count=1
131+
- Final candidate = 3
132+
133+
---
134+
135+
## Time and Space Complexity Summary
136+
- **Approach 1 (Brute Force):** O(n²) time, O(1) space
137+
- **Approach 2 (Sorting):** O(n log n) time, O(1)/O(log n) space
138+
- **Approach 3 (HashMap):** O(n) time, O(n) space
139+
- **Approach 4 (Voting Algorithm):** O(n) time, O(1) space
140+
141+
**Recommendation:** Use Approach 4 (Boyer-Moore Voting Algorithm) as it's the most optimal solution with linear time and constant space complexity.

0 commit comments

Comments
 (0)