|
| 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! 🚀** |
0 commit comments