Skip to content

Commit 7c949c9

Browse files
authored
Merge pull request #43 from iamAntimPal/Daily-Task
Daily task
2 parents 7e61f1a + 78aec05 commit 7c949c9

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def minOperationsToDistinct(nums):
2+
seen = set()
3+
count = 0
4+
while len(set(nums)) != len(nums):
5+
nums = nums[3:] # Remove first 3 elements
6+
count += 1
7+
return count
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# 📄 3396. Minimum Number of Operations to Make Elements in Array Distinct
2+
3+
## 📌 Problem Statement
4+
You are given an integer array **`nums`**. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:
5+
6+
### **Operations:**
7+
1. **Remove the first 3 elements** from the array. If the array has fewer than 3 elements, remove all remaining elements.
8+
2. Repeat this process until all elements in the array are distinct.
9+
3. The goal is to **find the minimum number of operations** needed.
10+
11+
---
12+
13+
## 🗂 **Constraints**
14+
- **`1 ≤ nums.length ≤ 100`**
15+
- **`1 ≤ nums[i] ≤ 100`**
16+
17+
---
18+
19+
## 🔢 **Example 1**
20+
21+
### **Input:**
22+
```python
23+
nums = [1,2,3,4,2,3,3,5,7]
24+
```
25+
### **Output:**
26+
```python
27+
2
28+
```
29+
### **Explanation:**
30+
1️⃣ **Remove first 3 elements:** `[4, 2, 3, 3, 5, 7]`
31+
2️⃣ **Remove next 3 elements:** `[3, 5, 7]` (distinct now)
32+
**Answer = `2`**
33+
34+
---
35+
36+
## 🔢 **Example 2**
37+
38+
### **Input:**
39+
```python
40+
nums = [4,5,6,4,4]
41+
```
42+
### **Output:**
43+
```python
44+
2
45+
```
46+
### **Explanation:**
47+
1️⃣ **Remove first 3 elements:** `[4, 4]`
48+
2️⃣ **Remove remaining elements:** `[]` (distinct now)
49+
**Answer = `2`**
50+
51+
---
52+
53+
## 🔢 **Example 3**
54+
55+
### **Input:**
56+
```python
57+
nums = [6,7,8,9]
58+
```
59+
### **Output:**
60+
```python
61+
0
62+
```
63+
### **Explanation:**
64+
The array is already distinct. ✅
65+
66+
---
67+
68+
## 🚀 **Optimized Approach**
69+
70+
### **Steps to Solve:**
71+
1️⃣ **Count duplicate elements** in the array.
72+
2️⃣ **Calculate the number of removals required** to eliminate duplicates.
73+
3️⃣ **Perform removal in groups of 3** to reach a distinct set efficiently.
74+
75+
---
76+
77+
## 🐍 **Python Solution**
78+
79+
```python
80+
def minOperationsToDistinct(nums):
81+
seen = set()
82+
count = 0
83+
while len(set(nums)) != len(nums):
84+
nums = nums[3:] # Remove first 3 elements
85+
count += 1
86+
return count
87+
88+
# Example Usage
89+
nums = [1,2,3,4,2,3,3,5,7]
90+
print(minOperationsToDistinct(nums)) # Output: 2
91+
```
92+
93+
---
94+
95+
## 💡 **Time Complexity Analysis**
96+
- **Checking for duplicates:** **`O(N)`**
97+
- **Removing elements:** **`O(N)`** (max 34 operations for `N=100`)
98+
99+
**Overall Complexity:** **`O(N)`** (Efficient for `N ≤ 100`)
100+
101+
---
102+
103+
## 📂 **File Structure**
104+
```
105+
MinOps-DistinctArray/
106+
│── 📁 problems/
107+
│ ├── 3396_min_operations.py
108+
│── README.md
109+
│── requirements.txt
110+
```
111+
112+
---
113+
114+
## 🔥 **Want to Participate?**
115+
🚀 Solve more **LeetCode Easy problems** in **Python**!
116+
117+
| # | Problem Name | Solution | LeetCode Link |
118+
| ---- | ----------------------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
119+
| 3396 | **Min Operations to Make Array Distinct** | [🔗 View](problems/3396_min_operations.py) | [🔗 LeetCode](https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/) |
120+
121+
---
122+
123+
## 🔗 **Useful Links**
124+
- 📘 [LeetCode Easy Problems](https://leetcode.com/problemset/all/?difficulty=Easy)
125+
- 🐍 [Python Set Operations](https://docs.python.org/3/library/stdtypes.html#set)
126+
- 🌟 [Contribute on GitHub](https://github.com/your-username/LeetCode_Easy_Problems)
127+
128+
---
129+
130+
💡 **Let's learn, code, and master easy problems together! 🚀**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def canPartition(self, nums: List[int]) -> bool:
3+
m, mod = divmod(sum(nums), 2)
4+
if mod:
5+
return False
6+
n = len(nums)
7+
f = [[False] * (m + 1) for _ in range(n + 1)]
8+
f[0][0] = True
9+
for i, x in enumerate(nums, 1):
10+
for j in range(m + 1):
11+
f[i][j] = f[i - 1][j] or (j >= x and f[i - 1][j - x])
12+
return f[n][m]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Partition Equal Subset Sum
2+
3+
## Problem Statement
4+
Given an integer array `nums`, return `true` if you can partition the array into two subsets such that the sum of the elements in both subsets is equal, otherwise return `false`.
5+
6+
## Examples
7+
8+
### Example 1:
9+
**Input:**
10+
```plaintext
11+
nums = [1,5,11,5]
12+
```
13+
**Output:**
14+
```plaintext
15+
true
16+
```
17+
**Explanation:**
18+
The array can be partitioned as `[1, 5, 5]` and `[11]`.
19+
20+
### Example 2:
21+
**Input:**
22+
```plaintext
23+
nums = [1,2,3,5]
24+
```
25+
**Output:**
26+
```plaintext
27+
false
28+
```
29+
**Explanation:**
30+
The array cannot be partitioned into equal sum subsets.
31+
32+
## Constraints
33+
- `1 <= nums.length <= 200`
34+
- `1 <= nums[i] <= 100`
35+
36+
## Approach
37+
The problem can be solved using **Dynamic Programming (Subset Sum Problem)**:
38+
1. Compute the total sum of the array.
39+
2. If the total sum is odd, return `false` (since it's impossible to split into two equal parts).
40+
3. Use a **0/1 Knapsack** approach to determine if a subset with sum `total_sum/2` exists.
41+
4. Use a **bottom-up DP approach** to check if we can form the required sum.
42+
43+
## Solution
44+
The solution uses a **boolean DP array** `dp[i]`, where `dp[i]` is `true` if a subset with sum `i` can be formed.
45+
- Initialize `dp[0] = true` (sum `0` is always possible).
46+
- Iterate over each number in `nums`, updating the `dp` array in reverse order.
47+
- If `dp[target]` is `true`, return `true`, otherwise return `false`.
48+
```python
49+
class Solution:
50+
def canPartition(self, nums: List[int]) -> bool:
51+
m, mod = divmod(sum(nums), 2)
52+
if mod:
53+
return False
54+
n = len(nums)
55+
f = [[False] * (m + 1) for _ in range(n + 1)]
56+
f[0][0] = True
57+
for i, x in enumerate(nums, 1):
58+
for j in range(m + 1):
59+
f[i][j] = f[i - 1][j] or (j >= x and f[i - 1][j - x])
60+
return f[n][m]
61+
```
62+
63+
## Complexity Analysis
64+
- **Time Complexity:** `O(n * sum/2)`, where `n` is the number of elements.
65+
- **Space Complexity:** `O(sum/2)`, optimized using a 1D DP array.
66+
67+
## Usage
68+
To run the solution, use the following function:
69+
```python
70+
from solution import canPartition
71+
print(canPartition([1,5,11,5])) # Output: True
72+
print(canPartition([1,2,3,5])) # Output: False
73+
```
74+

0 commit comments

Comments
 (0)