🔥 𝗗𝗮𝘆 𝟵𝟯/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟲𝟬𝟴. 𝗦𝗽𝗲𝗰𝗶𝗮𝗹 𝗔𝗿𝗿𝗮𝘆 𝗪𝗶𝘁𝗵 𝗫 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗼𝗿 𝗘𝗾𝘂𝗮𝗹 𝗫 | 🟢 Easy | Java A self-referential condition — x elements must be ≥ x. Elegant problem, elegant solution. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Find x such that exactly x elements in the array are ≥ x. Return -1 if no such x exists. ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗙𝗿𝗲𝗾𝘂𝗲𝗻𝗰𝘆 𝗖𝗼𝘂𝗻𝘁 + 𝗦𝘂𝗳𝗳𝗶𝘅 𝗦𝘂𝗺 ✅ Cap all values at n (array length) — anything larger contributes the same way ✅ Build a frequency count array of size n+1 ✅ Traverse from right to left, accumulating a running suffix sum ✅ When suffix sum == current index i → x = i is the answer! 💡 𝗪𝗵𝘆 𝗰𝗮𝗽 𝗮𝘁 𝗻? x can never exceed n (can't have more elements than the array size). So values above n are equivalent — capping them avoids index overflow and keeps the logic clean. 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(n) — two passes 📦 Space: O(n) — frequency array No sorting. No binary search. Just a clever frequency count + suffix accumulation. Sometimes the cleanest approach is right under your nose. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gm2c4-6x 𝟳 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗦𝗼 𝗰𝗹𝗼𝘀𝗲 𝘁𝗼 𝟭𝟬𝟬! 💪 #LeetCode #Day93of100 #100DaysOfCode #Java #DSA #Arrays #FrequencyCount #CodingChallenge #Programming
More Relevant Posts
-
Day 74 of #100DaysOfLeetCode 💻✅ Solved #162. Find Peak Element problem in Java. Approach: • Used Binary Search technique to efficiently find the peak element • Set two pointers, left at start and right at end of the array • Calculated mid index using safe mid formula • Compared nums[mid] with nums[mid + 1] to determine direction • If mid element is smaller, moved search space to right half • Otherwise, moved search space to left half including mid • Continued until left and right pointers converged • Final position (left == right) represents the peak index Performance: ✓ Runtime: 0 ms (Beats 100.00% submissions) 🚀 ✓ Memory: 44.32 MB (Beats 25.49% submissions) Key Learning: ✓ Strengthened understanding of Binary Search on unsorted arrays ✓ Learned how to apply divide-and-conquer beyond traditional searching ✓ Improved intuition for peak finding using neighbor comparison ✓ Practiced optimizing search space instead of linear scanning Learning one problem every single day 🚀 #Java #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 52/100 Today’s problem was based on String Manipulation — reversing the first k characters of a string. 🧠 What I learned: - How to efficiently manipulate strings - Using "StringBuilder" for reversal in Java - Writing clean and optimized code 💡 Approach: Reversed the first k characters and appended the remaining string. ⚡ Key Insight: Even simple problems help strengthen fundamentals, which are crucial for solving complex problems later. 👨💻 Code (Java): class Solution { public String reversePrefix(String s, int k) { String first = new StringBuilder(s.substring(0, k)).reverse().toString(); String rest = s.substring(k); return first + rest; } } 📈 Consistency is the key — showing up every day matters more than perfection. #Day52 #CodingChallenge #Java #DSA #LearningJourney #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟵𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟱𝟯𝟵. 𝗞𝘁𝗵 𝗠𝗶𝘀𝘀𝗶𝗻𝗴 𝗣𝗼𝘀𝗶𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿 | 🟢 Easy | Java Marked as Easy — but the optimal solution is pure binary search brilliance. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Given a sorted array, find the kth missing positive integer. Linear scan works — but can we do O(log n)? 💡 𝗧𝗵𝗲 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 At index i, the value arr[i] should be i+1 in a complete sequence. So missing numbers before arr[i] = arr[i] - 1 - i This lets us binary search on the count of missing numbers! ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 ✅ If arr[mid] - 1 - mid < k → not enough missing numbers yet, go right ✅ Else → too many missing, go left ✅ After the loop, left + k gives the exact answer 𝗪𝗵𝘆 𝗹𝗲𝗳𝘁 + 𝗸? After binary search, left is the index where the kth missing number falls beyond. left numbers exist in the array before that point, so the answer is left + k. No extra passes needed. ✨ 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(log n) — vs O(n) linear scan 📦 Space: O(1) This is a perfect example of binary searching on a derived condition, not just a value. A real upgrade from the naive approach. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gVYcjNS6 𝟲 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗧𝗵𝗲 𝗳𝗶𝗻𝗶𝘀𝗵 𝗹𝗶𝗻𝗲 𝗶𝘀 𝗿𝗶𝗴𝗵𝘁 𝘁𝗵𝗲𝗿𝗲! 🏁 #LeetCode #Day94of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
🔥 𝗗𝗮𝘆 𝟵𝟮/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟯𝟴𝟱. 𝗙𝗶𝗻𝗱 𝘁𝗵𝗲 𝗗𝗶𝘀𝘁𝗮𝗻𝗰𝗲 𝗩𝗮𝗹𝘂𝗲 𝗕𝗲𝘁𝘄𝗲𝗲𝗻 𝗧𝘄𝗼 𝗔𝗿𝗿𝗮𝘆𝘀 | 🟢 Easy | Java Could use brute force O(n×m) — chose binary search O(n log m) instead. Always optimise. 💡 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Count elements in arr1 where no element in arr2 is within distance d. That means |arr1[i] - arr2[j]| > d must hold for ALL j. ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗦𝗼𝗿𝘁 + 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 ✅ Sort arr2 once — O(m log m) ✅ For each element in arr1, binary search arr2 ✅ If any arr2[mid] falls within distance d → invalid, return false ✅ Navigate left/right based on comparison — if no match found → count it 💡 𝗪𝗵𝘆 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 𝗪𝗼𝗿𝗸𝘀 arr2 is sorted, so elements close in value are close in index. At each mid, if the distance is within d we immediately disqualify. Otherwise we confidently eliminate half the array. ✂️ 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(n log m + m log m) 📦 Space: O(1) extra The brute force is fine for small inputs — but building the binary search habit now pays dividends when constraints scale up. Always think: can I sort + search instead of nested loops? 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gYAnuwb2 𝟴 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗧𝗵𝗲 𝗰𝗲𝗻𝘁𝘂𝗿𝘆 𝗶𝘀 𝗰𝗮𝗹𝗹𝗶𝗻𝗴! 🏁 #LeetCode #Day92of100 #100DaysOfCode #Java #DSA #BinarySearch #Sorting #CodingChallenge #Programming
To view or add a comment, sign in
-
Day 65/75 — Count Pairs Whose Sum is Less than Target Today’s problem was a clean application of sorting + two pointers. Approach: • Sort the array • Use two pointers (i at start, j at end) • If nums[i] + nums[j] < target → all pairs between i and j are valid • Add (j - i) to count and move i forward • Otherwise, move j backward Key idea: if(nums.get(i) + nums.get(j) < target) count += (j - i); Time Complexity: O(n log n) (sorting) Space Complexity: O(1) This pattern shows up a lot in pair-based problems — very important to master. 65/75 🔥 #Day65 #DSA #TwoPointers #Sorting #Java #LeetCode
To view or add a comment, sign in
-
-
Some subarray counting problems become much easier when we transform them into “at most” problems. 🚀 Day 101/365 — DSA Challenge Solved: Binary Subarrays With Sum Problem idea: We need to count the number of subarrays whose sum equals a given goal in a binary array. Efficient approach: Instead of directly counting subarrays with exact sum, we use a trick: subarrays with sum = goal = subarrays with sum ≤ goal − subarrays with sum ≤ (goal − 1) Steps: 1. Create a helper function to count subarrays with sum at most k 2. Use a sliding window to maintain a valid window where sum ≤ k 3. Add the number of valid subarrays ending at each index 4. Subtract results to get the exact count for the goal This converts the problem into an efficient sliding window solution. ⏱ Time: O(n) 📦 Space: O(1) Day 101/365 complete. 💻 264 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟯: 𝗣𝗶𝘃𝗼𝘁𝗶𝗻𝗴 𝘄𝗵𝗲𝗻 𝘁𝗵𝗲 𝘀𝗶𝗺𝗽𝗹𝗲 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 𝗳𝗮𝗶𝗹𝘀. 🧠 The screenshot doesn’t show the struggle, It just shows the final Accepted code. ✅𝗙𝗼𝗿 𝗗𝗮𝘆 𝟯, I tackled this LeetCode problem: "𝟮𝟴𝟯𝟵. 𝗖𝗵𝗲𝗰𝗸 𝗶𝗳 𝗦𝘁𝗿𝗶𝗻𝗴𝘀 𝗖𝗮𝗻 𝗯𝗲 𝗠𝗮𝗱𝗲 𝗘𝗾𝘂𝗮𝗹 𝗪𝗶𝘁𝗵 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 𝗜." My goal was simple: just get that Java solution 𝗔𝗰𝗰𝗲𝗽𝘁𝗲𝗱. 𝗧𝗵𝗲 𝗙𝗶𝗿𝘀𝘁 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: I initially started with a classic iterative method, trying to loop through and swap indices to check equality. But I kept hitting unexpected logic errors that were overcomplicating a simple problem. I realized the "obvious" way wasn't the "correct" way. 𝗧𝗵𝗲 𝗣𝗶𝘃𝗼𝘁: Instead of getting stuck on the iteration, I looked at the core constraint. The operation only allows swaps between characters exactly 2 indices apart (e.g., indices 0 and 2, 1 and 3). 𝗧𝗵𝗲 𝗕𝗿𝗲𝗮𝗸𝘁𝗵𝗿𝗼𝘂𝗴𝗵: I realized that if strings are length 4, characters at even indices (0, 2) can only ever interact with each other. The same applies to odd indices (1, 3). So, instead of looping, I just needed to compare those two specific pairs directly. ✅𝗧𝗵𝗲 𝗿𝗲𝘀𝘂𝗹𝘁: I simplified the code into two simple comparison blocks and hit Submit. That final 𝟭𝗺𝘀 𝗥𝘂𝗻𝘁𝗶𝗺𝗲 (𝗕𝗲𝗮𝘁𝗶𝗻𝗴 𝟵𝟵.𝟲𝟲%) was the perfect confirmation that the pivot was the right choice. Sometimes the best engineering decision is to tear down what isn't working and start with a cleaner logic. #Java #JavaDeveloper #DSA #Strings #BuildInPublic #EngineeringStudent #DAY3
To view or add a comment, sign in
-
-
🚀 Day 75/100 Today’s problem: Remove Trailing Zeros from a Number (String) 💡 Problem Statement: Given a number in string format, remove all trailing zeros and return the result as a string. 🧠 Key Insight: No need to convert into integer (handles large inputs efficiently). Just traverse from the end and stop at the first non-zero character. 💻 Java Solution: class Solution { public String removeTrailingZeros(String num) { int i = num.length() - 1; while (i >= 0 && num.charAt(i) == '0') { i--; } return num.substring(0, i + 1); } } ⚡ Complexity: - Time: O(n) - Space: O(1) 📌 What I Learned: - String problems can often be solved without conversion - Understanding "substring()" is very important (end index is exclusive!) 🔥 Consistency is the key — showing up every day! #Day75 #Java #DSA #Coding #PlacementPreparation #Learning #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
Taking the Two-Pointer technique to LeetCode! Today's challenge: Remove Duplicates from a Sorted Array. After practicing the Two-Pointer pattern for reversing arrays and swapping 0s and 1s, I applied it to a classic LeetCode problem. The challenge? Remove duplicates from an array in-place with O(1) extra memory. Because the array is already sorted, all duplicates are grouped together. • The Slow Pointer (The Writer): Keeps track of where the next unique element should be placed. • The Fast Pointer (The Reader): Scans ahead through the array to find new, unique numbers. • The Logic: If the Reader finds a duplicate, it just skips it. But the moment the Reader finds a new number, the Writer records it at the front of the array and steps forward. #DSA #Java #LeetCode #RemoveDuplicate
To view or add a comment, sign in
-
-
Some of the hardest problems become manageable once you recognize a repeating pattern. 🚀 Day 105/365 — DSA Challenge Solved: Subarrays with K Different Integers Problem idea: We need to count subarrays that contain exactly k distinct integers. Efficient approach: Use the powerful trick: subarrays with exactly k distinct = subarrays with ≤ k distinct − subarrays with ≤ (k − 1) distinct Steps: 1. Use a sliding window with a hashmap to track frequency of elements 2. Expand window by moving right pointer 3. If distinct count exceeds k, shrink window from the left 4. Count valid subarrays ending at each index 5. Subtract results to get exact count This pattern converts a hard problem into a manageable one. ⏱ Time: O(n) 📦 Space: O(n) Day 105/365 complete. 💻 260 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #HashMap #LeetCode #LearningInPublic
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development