|
| 1 | +--- |
| 2 | +id: Most-Frequent-Even-Element |
| 3 | +title: Most Frequent Even Element |
| 4 | +sidebar_label: 2404-Most Frequent Even Element |
| 5 | +tags: [dsa, leetcode] |
| 6 | +description: Problem solution of Most Frequent Even Element |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +Given an integer array nums, return the most frequent even element. |
| 14 | + |
| 15 | +If there is a tie, return the smallest one. If there is no such element, return -1. |
| 16 | + |
| 17 | + |
| 18 | +### Examples |
| 19 | + |
| 20 | +#### Example 1 |
| 21 | +``` |
| 22 | +Input: nums = [0,1,2,2,4,4,1] |
| 23 | +Output: 2 |
| 24 | +Explanation: |
| 25 | +The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most. |
| 26 | +We return the smallest one, which is 2. |
| 27 | +``` |
| 28 | + |
| 29 | +### Example 2 |
| 30 | +``` |
| 31 | +Input: nums = [4,4,4,9,2,4] |
| 32 | +Output: 4 |
| 33 | +Explanation: 4 is the even element appears the most. |
| 34 | +``` |
| 35 | + |
| 36 | +### Example 3 |
| 37 | +``` |
| 38 | +Input: nums = [29,47,21,41,13,37,25,7] |
| 39 | +Output: -1 |
| 40 | +Explanation: There is no even element |
| 41 | +``` |
| 42 | + |
| 43 | +### Constraints |
| 44 | + |
| 45 | +-` 1 <= nums.length <= 2000` |
| 46 | +- `0 <= nums[i] <= 105` |
| 47 | + |
| 48 | +## Solution of Given Problem |
| 49 | + |
| 50 | +### Intuition and Approach |
| 51 | + |
| 52 | +The problem can be solved using a brute force approach or an optimized Technique. |
| 53 | + |
| 54 | +<Tabs> |
| 55 | +<tabItem value="Brute Force" label="Brute Force"> |
| 56 | + |
| 57 | +### Approach 1:Brute Force (Naive) |
| 58 | + |
| 59 | + |
| 60 | +Brute Force Approach: Iterate through the array and count the frequency of each even number using a dictionary or a similar data structure. |
| 61 | +Iterate through the frequency map to find the even number with the highest frequency. In case of a tie, select the smallest even number. |
| 62 | +#### Codes in Different Languages |
| 63 | + |
| 64 | +<Tabs> |
| 65 | +<TabItem value="C++" label="C++" default> |
| 66 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 67 | + |
| 68 | +```cpp |
| 69 | +#include <iostream> |
| 70 | +#include <vector> |
| 71 | +#include <unordered_map> |
| 72 | +using namespace std; |
| 73 | + |
| 74 | +int mostFrequentEven(vector<int>& nums) { |
| 75 | + unordered_map<int, int> freq; |
| 76 | + for (int num : nums) { |
| 77 | + if (num % 2 == 0) { |
| 78 | + freq[num]++; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + int maxFreq = 0; |
| 83 | + int res = -1; |
| 84 | + for (auto& [num, count] : freq) { |
| 85 | + if (count > maxFreq || (count == maxFreq && num < res)) { |
| 86 | + maxFreq = count; |
| 87 | + res = num; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return res; |
| 92 | +} |
| 93 | + |
| 94 | +int main() { |
| 95 | + vector<int> nums = {0, 1, 2, 2, 4, 4, 1}; |
| 96 | + cout << mostFrequentEven(nums) << endl; // Output: 2 |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +``` |
| 101 | +</TabItem> |
| 102 | +<TabItem value="Java" label="Java"> |
| 103 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 104 | +
|
| 105 | +```java |
| 106 | +import java.util.HashMap; |
| 107 | +
|
| 108 | +public class Main { |
| 109 | + public static int mostFrequentEven(int[] nums) { |
| 110 | + HashMap<Integer, Integer> freq = new HashMap<>(); |
| 111 | + for (int num : nums) { |
| 112 | + if (num % 2 == 0) { |
| 113 | + freq.put(num, freq.getOrDefault(num, 0) + 1); |
| 114 | + } |
| 115 | + } |
| 116 | +
|
| 117 | + int maxFreq = 0; |
| 118 | + int res = -1; |
| 119 | + for (int num : freq.keySet()) { |
| 120 | + int count = freq.get(num); |
| 121 | + if (count > maxFreq || (count == maxFreq && num < res)) { |
| 122 | + maxFreq = count; |
| 123 | + res = num; |
| 124 | + } |
| 125 | + } |
| 126 | +
|
| 127 | + return res; |
| 128 | + } |
| 129 | +
|
| 130 | + public static void main(String[] args) { |
| 131 | + int[] nums = {0, 1, 2, 2, 4, 4, 1}; |
| 132 | + System.out.println(mostFrequentEven(nums)); // Output: 2 |
| 133 | + } |
| 134 | +} |
| 135 | +
|
| 136 | + |
| 137 | +
|
| 138 | +
|
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | +</TabItem> |
| 143 | +<TabItem value="Python" label="Python"> |
| 144 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 145 | + |
| 146 | +```python |
| 147 | +from collections import defaultdict |
| 148 | + |
| 149 | +def most_frequent_even(nums): |
| 150 | + freq = defaultdict(int) |
| 151 | + for num in nums: |
| 152 | + if num % 2 == 0: |
| 153 | + freq[num] += 1 |
| 154 | + |
| 155 | + max_freq = 0 |
| 156 | + result = -1 |
| 157 | + for num, count in freq.items(): |
| 158 | + if count > max_freq or (count == max_freq and num < result): |
| 159 | + max_freq = count |
| 160 | + result = num |
| 161 | + |
| 162 | + return result |
| 163 | + |
| 164 | +nums = [0, 1, 2, 2, 4, 4, 1] |
| 165 | +print(most_frequent_even(nums)) # Output: 2 |
| 166 | + |
| 167 | +``` |
| 168 | + |
| 169 | +</TabItem> |
| 170 | +</Tabs> |
| 171 | + |
| 172 | + |
| 173 | +### Complexity Analysis |
| 174 | + |
| 175 | +- Time Complexity: $O(n^2)$ |
| 176 | +- due to the nested loop for counting frequency and finding the maximum. |
| 177 | +- Space Complexity: $O(n)$ |
| 178 | +- for storing the frequency of numbers. |
| 179 | + |
| 180 | +</tabItem> |
| 181 | +<tabItem value="Optimized approach" label="Optimized approach"> |
| 182 | + |
| 183 | +### Approach 2: Optimized approach |
| 184 | + |
| 185 | +Optimized Approach: Similar to the brute force method, but the counting and maximum frequency determination are optimized. |
| 186 | +Use a single pass to find the most frequent even element and track the maximum frequency simultaneously. |
| 187 | + |
| 188 | +#### Code in Different Languages |
| 189 | + |
| 190 | +<Tabs> |
| 191 | +<TabItem value="C++" label="C++" default> |
| 192 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 193 | + |
| 194 | +```cpp |
| 195 | +#include <iostream> |
| 196 | +#include <vector> |
| 197 | +#include <unordered_map> |
| 198 | +using namespace std; |
| 199 | + |
| 200 | +int mostFrequentEven(vector<int>& nums) { |
| 201 | + unordered_map<int, int> freq; |
| 202 | + int maxFreq = 0; |
| 203 | + int res = -1; |
| 204 | + |
| 205 | + for (int num : nums) { |
| 206 | + if (num % 2 == 0) { |
| 207 | + freq[num]++; |
| 208 | + if (freq[num] > maxFreq || (freq[num] == maxFreq && num < res)) { |
| 209 | + maxFreq = freq[num]; |
| 210 | + res = num; |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + return res; |
| 216 | +} |
| 217 | + |
| 218 | +int main() { |
| 219 | + vector<int> nums = {0, 1, 2, 2, 4, 4, 1}; |
| 220 | + cout << mostFrequentEven(nums) << endl; // Output: 2 |
| 221 | + return 0; |
| 222 | +} |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | +``` |
| 227 | +</TabItem> |
| 228 | +<TabItem value="Java" label="Java"> |
| 229 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 230 | +
|
| 231 | +```java |
| 232 | +import java.util.HashMap; |
| 233 | +
|
| 234 | +public class Main { |
| 235 | + public static int mostFrequentEven(int[] nums) { |
| 236 | + HashMap<Integer, Integer> freq = new HashMap<>(); |
| 237 | + int maxFreq = 0; |
| 238 | + int res = -1; |
| 239 | +
|
| 240 | + for (int num : nums) { |
| 241 | + if (num % 2 == 0) { |
| 242 | + freq.put(num, freq.getOrDefault(num, 0) + 1); |
| 243 | + if (freq.get(num) > maxFreq || (freq.get(num) == maxFreq && num < res)) { |
| 244 | + maxFreq = freq.get(num); |
| 245 | + res = num; |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | +
|
| 250 | + return res; |
| 251 | + } |
| 252 | +
|
| 253 | + public static void main(String[] args) { |
| 254 | + int[] nums = {0, 1, 2, 2, 4, 4, 1}; |
| 255 | + System.out.println(mostFrequentEven(nums)); // Output: 2 |
| 256 | + } |
| 257 | +} |
| 258 | +
|
| 259 | +
|
| 260 | +``` |
| 261 | + |
| 262 | + |
| 263 | +</TabItem> |
| 264 | +<TabItem value="Python" label="Python"> |
| 265 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 266 | + |
| 267 | +```python |
| 268 | +from collections import defaultdict |
| 269 | + |
| 270 | +def most_frequent_even(nums): |
| 271 | + freq = defaultdict(int) |
| 272 | + max_freq = 0 |
| 273 | + result = -1 |
| 274 | + |
| 275 | + for num in nums: |
| 276 | + if num % 2 == 0: |
| 277 | + freq[num] += 1 |
| 278 | + if freq[num] > max_freq or (freq[num] == max_freq and num < result): |
| 279 | + max_freq = freq[num] |
| 280 | + result = num |
| 281 | + |
| 282 | + return result |
| 283 | + |
| 284 | +nums = [0, 1, 2, 2, 4, 4, 1] |
| 285 | +print(most_frequent_even(nums)) # Output: 2 |
| 286 | + |
| 287 | + |
| 288 | +``` |
| 289 | + |
| 290 | +</TabItem> |
| 291 | +</Tabs> |
| 292 | + |
| 293 | +#### Complexity Analysis |
| 294 | + |
| 295 | +- Time Complexity: $O(n)$ |
| 296 | +- due to a single pass through the array. |
| 297 | +- Space Complexity: $O(n)$ |
| 298 | +- for storing the frequency map. |
| 299 | +- This approach is efficient and straightforward. |
| 300 | + |
| 301 | +</tabItem> |
| 302 | +</Tabs> |
| 303 | + |
| 304 | + |
| 305 | +## Video Explanation of Given Problem |
| 306 | + |
| 307 | + <LiteYouTubeEmbed |
| 308 | + id="5nRxQGWZy0g" |
| 309 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 310 | + title="Problem Explanation | Solution | Approach" |
| 311 | + poster="maxresdefault" |
| 312 | + webp |
| 313 | + /> |
| 314 | + |
| 315 | +--- |
| 316 | + |
| 317 | +<h2>Authors:</h2> |
| 318 | + |
| 319 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 320 | +{['AmruthaPariprolu'].map(username => ( |
| 321 | + <Author key={username} username={username} /> |
| 322 | +))} |
| 323 | +</div> |
| 324 | + |
0 commit comments