From 8977ff1880d36ca758e7eef505be9e03a964ba40 Mon Sep 17 00:00:00 2001 From: Samuel Joseph Date: Sun, 30 Nov 2025 14:38:00 +0530 Subject: [PATCH 1/4] Solution #1394 - Samuel Joseph - 30.11.25 - commit 1 --- .../explanation.md | 15 +++++++++++++ .../solution.cpp | 21 +++++++++++++++++++ .../solution.java | 19 +++++++++++++++++ .../solution.py | 18 ++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md create mode 100644 Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.cpp create mode 100644 Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.java create mode 100644 Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.py diff --git a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md new file mode 100644 index 0000000..fcddfb6 --- /dev/null +++ b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md @@ -0,0 +1,15 @@ +### What this code is logically doing + +- Declares three variables at the start of the method: + - `temp` is used as a temporary holder for the current array element being analyzed in the outer loop. + - `lucky` starts at `-1` and is meant to store a “special” value if the condition in the loop is ever satisfied; if it is never updated, `-1` is returned to indicate “no such value found”. + - `count` keeps track of how many times the current `temp` value occurs in the array during each pass of the inner loop. + +- Uses a nested loop structure to count frequencies: + - The outer loop `for (int i = 0; i < arr.length; i++)` picks each element `arr[i]` one by one and assigns it to `temp`, then resets `count` to `0` for that new value. + - The inner loop `for (int j = 0; j < arr.length; j++)` scans the entire array from start to end and compares every `arr[j]` with `temp`; whenever a match is found, `count` is incremented, so by the end of the inner loop `count` equals the total number of occurrences of `temp` in the array. + +- After finishing the inner loop for a given `temp`, it evaluates `if (count == arr.length)`: + - This condition checks whether the current value `temp` appears exactly as many times as the total number of elements in the array, which effectively means: “Is every element in the array equal to `temp`?”. + - If this condition is true, the code sets `lucky = temp`, so `lucky` is updated to this value for that iteration. + - Because this check happens for every `i`, if the condition becomes true multiple times (for example, when all elements are the same), `lucky` will be reassigned each time, and the value ultimately returned from the function will be the value from the **last** outer-loop iteration where `count == arr.length` held true, determined purely by the order of elements in the array. diff --git a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.cpp b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.cpp new file mode 100644 index 0000000..492e756 --- /dev/null +++ b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int findLucky(vector& arr) { + int temp = 0, lucky = -1, count = 0; + + for (int i = 0; i < arr.size(); i++) { + count = 0; + temp = arr[i]; + + for (int j = 0; j < arr.size(); j++) { + if (arr[j] == temp) + count++; + } + + if (count == arr.size()) + lucky = temp; + } + + return lucky; + } +}; diff --git a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.java b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.java new file mode 100644 index 0000000..da9285f --- /dev/null +++ b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.java @@ -0,0 +1,19 @@ +class Solution { + public int findLucky(int[] arr) + { + int temp=0,lucky=-1,count=0; + for(int i=0;ilucky) + lucky=temp; + } + return lucky; + } +} \ No newline at end of file diff --git a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.py b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.py new file mode 100644 index 0000000..8fec7da --- /dev/null +++ b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/solution.py @@ -0,0 +1,18 @@ +class Solution: + def findLucky(self, arr): + temp = 0 + lucky = -1 + count = 0 + + for i in range(len(arr)): + count = 0 + temp = arr[i] + + for j in range(len(arr)): + if arr[j] == temp: + count += 1 + + if count == len(arr): + lucky = temp + + return lucky \ No newline at end of file From 9e9d0eac4ea52c382a08e700a1deb40bde9fa528 Mon Sep 17 00:00:00 2001 From: Samuel Joseph Date: Mon, 1 Dec 2025 10:59:08 +0530 Subject: [PATCH 2/4] Updated Explanation --- .../explanation.md | 215 +++++++++++++++++- 1 file changed, 203 insertions(+), 12 deletions(-) diff --git a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md index fcddfb6..20dd32c 100644 --- a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md +++ b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md @@ -1,15 +1,206 @@ -### What this code is logically doing +# 1394. Find Lucky Integer in an Array -- Declares three variables at the start of the method: - - `temp` is used as a temporary holder for the current array element being analyzed in the outer loop. - - `lucky` starts at `-1` and is meant to store a “special” value if the condition in the loop is ever satisfied; if it is never updated, `-1` is returned to indicate “no such value found”. - - `count` keeps track of how many times the current `temp` value occurs in the array during each pass of the inner loop. +**Difficulty:** Easy +**Category:** Arrays, Hashing +**Leetcode Link:** https://leetcode.com/problems/find-lucky-integer-in-an-array/ [web:18] -- Uses a nested loop structure to count frequencies: - - The outer loop `for (int i = 0; i < arr.length; i++)` picks each element `arr[i]` one by one and assigns it to `temp`, then resets `count` to `0` for that new value. - - The inner loop `for (int j = 0; j < arr.length; j++)` scans the entire array from start to end and compares every `arr[j]` with `temp`; whenever a match is found, `count` is incremented, so by the end of the inner loop `count` equals the total number of occurrences of `temp` in the array. +--- -- After finishing the inner loop for a given `temp`, it evaluates `if (count == arr.length)`: - - This condition checks whether the current value `temp` appears exactly as many times as the total number of elements in the array, which effectively means: “Is every element in the array equal to `temp`?”. - - If this condition is true, the code sets `lucky = temp`, so `lucky` is updated to this value for that iteration. - - Because this check happens for every `i`, if the condition becomes true multiple times (for example, when all elements are the same), `lucky` will be reassigned each time, and the value ultimately returned from the function will be the value from the **last** outer-loop iteration where `count == arr.length` held true, determined purely by the order of elements in the array. +## 📝 Introduction + +You are given an integer array `arr`. +A *lucky integer* is defined as an integer whose **value is equal to its frequency** in the array. +The task is to return the **largest** lucky integer, or `-1` if no such integer exists. [web:18][web:2] + +Key points and constraints: +- `1 <= arr.length <= 500` +- `1 <= arr[i] <= 500` +- Need to compare each value with how many times it appears and pick the maximum such value. [web:2][web:40] + +--- + +## 💡 Approach & Key Insights + +Main idea: +1. Count how many times each number appears in the array (its frequency). +2. A number is *lucky* if `value == frequency`. +3. Among all lucky numbers, return the **largest** one. [web:16][web:4] + +Insights: +- A brute force solution can compute frequency for each element by scanning the array repeatedly (nested loops). +- An optimal solution uses a frequency structure (hash map or counting array) to count in linear time, then scans the frequency structure to find the largest value satisfying `value == frequency`. [web:16][web:40] + +--- + +## 🛠️ Breakdown of Approaches + +### 1️⃣ Brute Force / Naive Approach + +- **Explanation:** + - For each element `arr[i]`, iterate over the entire array and count how many times this value appears. + - After counting, if `count == arr[i]`, then `arr[i]` is a lucky integer candidate. + - Track the **maximum** such candidate across all elements. + - This is exactly the logic you wrote in Java/Python/C++ with nested loops (just fix the condition to `count == temp` and keep max). [web:16][web:32] + +- **Time Complexity:** + - Outer loop runs `n` times, inner loop runs `n` times for each outer iteration. + - Overall \(O(n^2)\) time. [web:31][web:33] + +- **Space Complexity:** + - Uses only a few integer variables (`temp`, `count`, `lucky`). + - Overall \(O(1)\) extra space. [web:31] + +- **Example/Dry Run:** + +Example input: `arr = [2, 2, 3, 4]` [web:2][web:40] + +- Step 1: `i = 0`, `temp = 2` + - Count frequency of `2` by scanning entire array → `count = 2`. + - Check `count == temp` → `2 == 2` → lucky candidate = 2, `lucky = 2`. + +- Step 2: `i = 1`, `temp = 2` again + - Frequency still 2, `count == temp` → `lucky` remains 2. + +- Step 3: `i = 2`, `temp = 3` + - Count frequency of `3` → `count = 1`, `count != temp`, ignore. + +- Step 4: `i = 3`, `temp = 4` + - Count frequency of `4` → `count = 1`, `count != temp`, ignore. + +Output: `2`. + +--- + +### 2️⃣ Optimized Approach + +- **Explanation:** + - Instead of recounting each value with nested loops, use a **frequency map/array**. + - First pass: build `freq[x] = frequency of x` for all elements in `arr`. + - Second pass: iterate over possible values and check where `value == freq[value]`. + - Track the **maximum** value that satisfies this condition. [web:16][web:4][web:40] + + Because constraints are small (`arr[i] <= 500`), a simple integer array of size `501` is enough for frequencies. [web:2][web:16] + +- **Time Complexity:** + - Count frequencies in \(O(n)\). + - Scan possible values (at most 500) in \(O(K)\) where `K = 500`. + - Total \(O(n + K)\), effectively \(O(n)\) for given constraints. [web:16][web:40] + +- **Space Complexity:** + - Frequency array of fixed size `501`. + - Overall \(O(K)\), which is \(O(1)\) relative to `n` (because K is bounded by constraints). [web:16][web:46] + +- **Example/Dry Run:** + +Example input: `arr = [1, 2, 2, 3, 3, 3]` [web:2][web:40] + +- Step 1: Count frequencies: + - `freq[1] = 1`, `freq[2] = 2`, `freq[3] = 3`, all others 0. +- Step 2: Check values from high to low: + - `i = 6, 5, 4` → `freq[i]` not equal to `i`. + - `i = 3` → `freq[3] = 3` → lucky, candidate = 3. + - `i = 2` → `freq[2] = 2` → also lucky, but smaller than 3. + - `i = 1` → `freq[1] = 1` → also lucky, but smaller than 3. +- Step 3: Return the largest lucky integer seen → `3`. + +Output: `3`. + +--- + +### 3️⃣ Best / Final Optimized Approach (if applicable) + +For this problem, the **counting array or hashmap frequency solution** is already optimal in both time and space under the given constraints. [web:16][web:4][web:10] + +- **Explanation:** + - Use a hash map or fixed-size array to count all frequencies in a single pass. + - Then either: + - Scan from largest possible value to smallest (guarantees the first match is the answer), or + - Collect all lucky integers and take their maximum. + - This approach is simple, fast, and directly encodes the problem definition `value == frequency`. [web:16][web:46] + +- **Time Complexity:** + - \(O(n)\) to build frequencies + \(O(K)\) to scan, where `K` is bounded (≤ 500). + - Overall \(O(n)\) for practical purposes. [web:16][web:40] + +- **Space Complexity:** + - \(O(K)\) for the frequency structure, effectively constant. [web:16][web:46] + +- **Example/Dry Run:** + +Example input: `arr = [2, 2, 2, 3, 3]` [web:2][web:40] + +- Step 1: Build frequency: + - `freq[2] = 3`, `freq[3] = 2`. +- Step 2: Scan from high to low: + - `i = 5, 4` → `freq[i] = 0`, not equal to `i`. + - `i = 3` → `freq[3] = 2`, `2 != 3` → not lucky. + - `i = 2` → `freq[2] = 3`, `3 != 2` → not lucky. + - `i = 1` → `freq[1] = 0`, not lucky. +- No lucky integer found → return `-1`. + +Output: `-1`. + +--- + +## 📊 Complexity Analysis + +| Approach | Time Complexity | Space Complexity | +| ------------- | -----------------| ---------------- | +| Brute Force | O(n²) | O(1) | +| Optimized | O(n + K) ≈ O(n) | O(K) (K ≤ 500) | +| Best Approach | O(n + K) ≈ O(n) | O(K) (K ≤ 500) | [web:16][web:46] + +--- + +## 📉 Optimization Ideas + +- Because `arr[i]` is at most 500, using a **fixed-size array** is more cache-friendly and simpler than a generic hash map. [web:2][web:16] +- Scanning from the **maximum possible value downwards** allows early exit as soon as the largest lucky integer is found. [web:16][web:46] +- In languages with built-in frequency utilities (like `Counter` in Python), code can be both concise and efficient while still following the same logic. [web:16] + +--- + +## 📌 Example Walkthroughs & Dry Runs +``` +Example 1: +Input:​ + +Frequencies: +2 -> 2 times +3 -> 1 time +4 -> 1 time + +Lucky check: +2: value 2, frequency 2 -> lucky +3: value 3, frequency 1 -> not lucky +4: value 4, frequency 1 -> not lucky + +Largest lucky integer = 2 +Output: 2 + +Example 2: +Input:​ + +Frequencies: +1 -> 1 time +2 -> 2 times +3 -> 3 times + +Lucky integers: 1, 2, 3 (all satisfy value == frequency) +Largest lucky integer = 3 +Output: 3 +``` + +--- + +## 🔗 Additional Resources + +- [LeetCode Problem 1394 – Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/) +- [In-depth explanation with frequency map and code examples](https://algo.monster/liteproblems/1394) +- [Alternative editorial and multi-language solutions](https://progiez.com/1394-find-lucky-integer-in-an-array-leetcode-solution) +- [Another clean solution write-up](https://walkccc.me/LeetCode/problems/1394/) + +--- + +Author: *Samuel Joseph S* +Date: *01/12/2025* \ No newline at end of file From df07e3bd5285bd30839c53ac2ea85d5b88ad94ce Mon Sep 17 00:00:00 2001 From: Samuel Joseph Date: Mon, 1 Dec 2025 11:07:05 +0530 Subject: [PATCH 3/4] Update explanation.md --- .../explanation.md | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md index 20dd32c..8fd6616 100644 --- a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md +++ b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md @@ -2,7 +2,7 @@ **Difficulty:** Easy **Category:** Arrays, Hashing -**Leetcode Link:** https://leetcode.com/problems/find-lucky-integer-in-an-array/ [web:18] +**Leetcode Link:** https://leetcode.com/problems/find-lucky-integer-in-an-array/ --- @@ -10,12 +10,12 @@ You are given an integer array `arr`. A *lucky integer* is defined as an integer whose **value is equal to its frequency** in the array. -The task is to return the **largest** lucky integer, or `-1` if no such integer exists. [web:18][web:2] +The task is to return the **largest** lucky integer, or `-1` if no such integer exists. Key points and constraints: - `1 <= arr.length <= 500` - `1 <= arr[i] <= 500` -- Need to compare each value with how many times it appears and pick the maximum such value. [web:2][web:40] +- Need to compare each value with how many times it appears and pick the maximum such value. --- @@ -24,12 +24,11 @@ Key points and constraints: Main idea: 1. Count how many times each number appears in the array (its frequency). 2. A number is *lucky* if `value == frequency`. -3. Among all lucky numbers, return the **largest** one. [web:16][web:4] +3. Among all lucky numbers, return the **largest** one. Insights: - A brute force solution can compute frequency for each element by scanning the array repeatedly (nested loops). -- An optimal solution uses a frequency structure (hash map or counting array) to count in linear time, then scans the frequency structure to find the largest value satisfying `value == frequency`. [web:16][web:40] - +- An optimal solution uses a frequency structure (hash map or counting array) to count in linear time, then scans the frequency structure to find the largest value satisfying `value == frequency`. --- ## 🛠️ Breakdown of Approaches @@ -40,19 +39,18 @@ Insights: - For each element `arr[i]`, iterate over the entire array and count how many times this value appears. - After counting, if `count == arr[i]`, then `arr[i]` is a lucky integer candidate. - Track the **maximum** such candidate across all elements. - - This is exactly the logic you wrote in Java/Python/C++ with nested loops (just fix the condition to `count == temp` and keep max). [web:16][web:32] - + - This is exactly the logic you wrote in Java/Python/C++ with nested loops (just fix the condition to `count == temp` and keep max). - **Time Complexity:** - Outer loop runs `n` times, inner loop runs `n` times for each outer iteration. - - Overall \(O(n^2)\) time. [web:31][web:33] + - Overall \(O(n^2)\) time. - **Space Complexity:** - Uses only a few integer variables (`temp`, `count`, `lucky`). - - Overall \(O(1)\) extra space. [web:31] + - Overall \(O(1)\) extra space. - **Example/Dry Run:** -Example input: `arr = [2, 2, 3, 4]` [web:2][web:40] +Example input: `arr = [2, 2, 3, 4]` - Step 1: `i = 0`, `temp = 2` - Count frequency of `2` by scanning entire array → `count = 2`. @@ -79,20 +77,20 @@ Output: `2`. - Second pass: iterate over possible values and check where `value == freq[value]`. - Track the **maximum** value that satisfies this condition. [web:16][web:4][web:40] - Because constraints are small (`arr[i] <= 500`), a simple integer array of size `501` is enough for frequencies. [web:2][web:16] + Because constraints are small (`arr[i] <= 500`), a simple integer array of size `501` is enough for frequencies. - **Time Complexity:** - Count frequencies in \(O(n)\). - Scan possible values (at most 500) in \(O(K)\) where `K = 500`. - - Total \(O(n + K)\), effectively \(O(n)\) for given constraints. [web:16][web:40] + - Total \(O(n + K)\), effectively \(O(n)\) for given constraints. - **Space Complexity:** - Frequency array of fixed size `501`. - - Overall \(O(K)\), which is \(O(1)\) relative to `n` (because K is bounded by constraints). [web:16][web:46] + - Overall \(O(K)\), which is \(O(1)\) relative to `n` (because K is bounded by constraints). - **Example/Dry Run:** -Example input: `arr = [1, 2, 2, 3, 3, 3]` [web:2][web:40] +Example input: `arr = [1, 2, 2, 3, 3, 3]` - Step 1: Count frequencies: - `freq[1] = 1`, `freq[2] = 2`, `freq[3] = 3`, all others 0. @@ -109,25 +107,25 @@ Output: `3`. ### 3️⃣ Best / Final Optimized Approach (if applicable) -For this problem, the **counting array or hashmap frequency solution** is already optimal in both time and space under the given constraints. [web:16][web:4][web:10] +For this problem, the **counting array or hashmap frequency solution** is already optimal in both time and space under the given constraints. - **Explanation:** - Use a hash map or fixed-size array to count all frequencies in a single pass. - Then either: - Scan from largest possible value to smallest (guarantees the first match is the answer), or - Collect all lucky integers and take their maximum. - - This approach is simple, fast, and directly encodes the problem definition `value == frequency`. [web:16][web:46] + - This approach is simple, fast, and directly encodes the problem definition `value == frequency`. - **Time Complexity:** - \(O(n)\) to build frequencies + \(O(K)\) to scan, where `K` is bounded (≤ 500). - - Overall \(O(n)\) for practical purposes. [web:16][web:40] + - Overall \(O(n)\) for practical purposes. - **Space Complexity:** - - \(O(K)\) for the frequency structure, effectively constant. [web:16][web:46] + - \(O(K)\) for the frequency structure, effectively constant. - **Example/Dry Run:** -Example input: `arr = [2, 2, 2, 3, 3]` [web:2][web:40] +Example input: `arr = [2, 2, 2, 3, 3]` - Step 1: Build frequency: - `freq[2] = 3`, `freq[3] = 2`. @@ -148,15 +146,15 @@ Output: `-1`. | ------------- | -----------------| ---------------- | | Brute Force | O(n²) | O(1) | | Optimized | O(n + K) ≈ O(n) | O(K) (K ≤ 500) | -| Best Approach | O(n + K) ≈ O(n) | O(K) (K ≤ 500) | [web:16][web:46] +| Best Approach | O(n + K) ≈ O(n) | O(K) (K ≤ 500) | --- ## 📉 Optimization Ideas -- Because `arr[i]` is at most 500, using a **fixed-size array** is more cache-friendly and simpler than a generic hash map. [web:2][web:16] -- Scanning from the **maximum possible value downwards** allows early exit as soon as the largest lucky integer is found. [web:16][web:46] -- In languages with built-in frequency utilities (like `Counter` in Python), code can be both concise and efficient while still following the same logic. [web:16] +- Because `arr[i]` is at most 500, using a **fixed-size array** is more cache-friendly and simpler than a generic hash map. +- Scanning from the **maximum possible value downwards** allows early exit as soon as the largest lucky integer is found. +- In languages with built-in frequency utilities (like `Counter` in Python), code can be both concise and efficient while still following the same logic. --- From eaff7e4a964b3c91930b542650e482482db03e87 Mon Sep 17 00:00:00 2001 From: Samuel Joseph Date: Mon, 1 Dec 2025 11:14:41 +0530 Subject: [PATCH 4/4] Updated explanation --- .../#1394 - Lucky integer in an Array - Easy/explanation.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md index 8fd6616..fdbda21 100644 --- a/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md +++ b/Arrays & Strings/#1394 - Lucky integer in an Array - Easy/explanation.md @@ -38,8 +38,7 @@ Insights: - **Explanation:** - For each element `arr[i]`, iterate over the entire array and count how many times this value appears. - After counting, if `count == arr[i]`, then `arr[i]` is a lucky integer candidate. - - Track the **maximum** such candidate across all elements. - - This is exactly the logic you wrote in Java/Python/C++ with nested loops (just fix the condition to `count == temp` and keep max). + - Track the **maximum** such candidate across all elements. - **Time Complexity:** - Outer loop runs `n` times, inner loop runs `n` times for each outer iteration. - Overall \(O(n^2)\) time. @@ -195,8 +194,7 @@ Output: 3 - [LeetCode Problem 1394 – Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/) - [In-depth explanation with frequency map and code examples](https://algo.monster/liteproblems/1394) -- [Alternative editorial and multi-language solutions](https://progiez.com/1394-find-lucky-integer-in-an-array-leetcode-solution) -- [Another clean solution write-up](https://walkccc.me/LeetCode/problems/1394/) +- [Alternative editorial and multi-language solutions](https://progiez.com/1394-find-lucky-integer-in-an-array-leetcode-solution) ---