#Day9 of my #100DaysCodingChallenge 💻 Today’s problem pushed my reasoning around frequency analysis and window-based computation — a brilliant mix of hashmaps and sliding window logic! 🧮⚙️ 🔍 Problem Explored: Find X-Sum of All K-Long Subarrays — where for every subarray of length k, you need to compute the sum of the top x most frequent (and largest, if tied) elements. 💡 Approach: For each subarray, counted element frequencies using a HashMap, then sorted entries by frequency (and value on ties) to pick the top x contributors. Finally, optimized the logic with sliding windows to reuse previous computations efficiently. ⏱ Time Complexity: O(n × k log k) (initial) → optimizable using ordered maps or heaps 🧠 Space Complexity: O(k) ✅ Result: Implemented a clear and correct Java solution that balances frequency tracking with subarray iteration — a great hands-on dive into map manipulation and priority-based logic. 📘 Learning: When dealing with frequency-based problems, structuring data efficiently is half the battle. A well-placed map or heap can turn a brute-force approach into an elegant one. 🙌 Special thanks to K.R. Mangalam University and Abhishek Kumar Sir for continuously motivating me to refine my logic and efficiency. #Java #CodingChallenge #100DaysOfCode #ProblemSolving #LeetCode #CompetitiveProgramming #DSA #InterviewPrep #SlidingWindow #HashMap #FrequencyAnalysis #CodingJourney #LearningEveryday
Gaurav Sharma’s Post
More Relevant Posts
-
🚀 LeetCode Progress Update: Invert Binary Tree (Problem 226) 🌳 About the Problem: The task was to invert a binary tree — flipping it by swapping every left and right child node. It’s a classic problem that tests recursion and tree traversal logic. 🧠 My Approach: I went with a recursive approach. At each node, I swapped the left and right subtrees, then called the same logic for both sides. Once the node became null, the recursion stopped automatically. 💡 What I Learned: ✅ How recursion travels through a tree structure ✅ Why defining a clear base condition matters ✅ How simple logic can transform an entire data structure 🎯 Takeaway: This problem reinforced that not every challenge needs complexity — sometimes, the cleanest recursive idea is the smartest one. #LeetCode #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
📌 Day 8/100 - Search Insert Position (LeetCode 35) 🔹 Problem: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be inserted in order. 🔹 Approach: I used a binary search approach for efficiency 🔍 1️⃣ Start with two pointers — low and high. 2️⃣ Find the mid index and compare nums[mid] with the target. 3️⃣ If target equals nums[mid], return mid. 4️⃣ If target is smaller, move the high pointer left. 5️⃣ If target is greater, move the low pointer right. 6️⃣ When the loop ends, low gives the correct insert position. 🔹 Key Learning: Binary Search saves time — reducing O(n) to O(log n)! Understanding the condition when to move left/right is key. Even simple problems sharpen logical precision and boundary handling. Each problem strengthens the logic muscle 🧠 — one step closer to mastering algorithms! 💪 #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #LearnByDoing
To view or add a comment, sign in
-
-
🗓 Day 8/ 100 – #100DaysOfLeetCode 📌 Problem 3234: Count the Number of Substrings With Dominant Ones A substring is said to have dominant ones if: 👉 #1s ≥ (#0s)² The task is to count how many substrings in the binary string satisfy this condition. 🧠 My Approach: 🔹 Iterated through substrings while maintaining counts of zeros and ones. 🔹 Used the condition ones ≥ zeros² to determine whether a substring is valid. 🔹 Applied early stopping when the zero count became too large, since the quadratic requirement makes dominance increasingly difficult to achieve. 🔹 This pruning significantly reduced unnecessary checks and improved the overall efficiency. ⏱ Time & Space Complexity Time Complexity: O(n · √n) Because for each starting index, we only explore substrings until the zero count reaches ~√n (beyond which zeros² becomes too large to satisfy). This is a major improvement over the brute-force O(n²). Space Complexity: O(1) Only uses a few counters (ones, zeros, indices). 💡 Key Learning: This problem beautifully shows how mathematical constraints can simplify substring evaluation. Recognizing that zeros affect the condition quadratically helped guide a smarter pruning strategy, turning an expensive brute-force check into something efficient and elegant. #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🔢 Day 47 of #LeetCode100DaysChallenge Solved LeetCode 79: Word Search — a classic backtracking problem that beautifully blends recursion and grid traversal. 🧩 Problem: Given a 2D grid of characters and a word, determine if the word can be formed using sequentially adjacent cells (up, down, left, right), where each cell can be used only once. 💡 Approach Used — Backtracking (DFS): 1️⃣ Start from each cell that matches the first character. 2️⃣ Explore in all four directions recursively. 3️⃣ Temporarily mark visited cells to avoid reuse. 4️⃣ If the entire word is matched, return true; otherwise, backtrack. ⚙️ Complexity: Time: O(N × 4ᴸ) — where N is the total number of cells, and L is the word length. Space: O(L) — recursion depth. ✨ Key Takeaways: ✅ Strengthened understanding of recursion and backtracking. ✅ Learned to manage visited states effectively in grid problems. ✅ Great exercise in applying DFS to real-world matrix traversal cases. #LeetCode #100DaysOfCode #Java #Backtracking #Recursion #ProblemSolving #DSA #WomenInTech #CodingJourney
To view or add a comment, sign in
-
-
💡 Day 57 of #100DaysOfCode 💡 Today, I solved LeetCode Problem #217 – Contains Duplicate 🧩 Given an integer array, the task is simple yet fundamental: 👉 Determine whether any value appears at least twice in the array. 💻 Language: Java ⚡ Runtime: 13 ms — Beats 87.70% 🚀 📉 Memory: 58.28 MB — Beats 63.02% 🧠 Key Learnings: Reinforced my understanding of HashSet and its O(1) lookup time. Learned how sets help efficiently identify duplicates in large datasets. Strengthened array traversal and data structure optimization skills. 🧩 Approach: 1️⃣ Initialize an empty HashSet. 2️⃣ Iterate through each element in the array. 3️⃣ If an element already exists in the set → return true. 4️⃣ Else, add it to the set. 5️⃣ If loop completes, return false. ✅ Complexity: Time: O(n) Space: O(n) ✨ Takeaway: Even basic problems like these teach the power of hash-based structures — simple yet powerful for building optimized systems and preventing redundant computations. #100DaysOfCode #LeetCode #Java #DataStructures #HashSet #ProblemSolving #CodingChallenge #CleanCode #Programming #SoftwareEngineering #TechLearning #Algorithms
To view or add a comment, sign in
-
-
✅ Just solved LeetCode #654 — Maximum Binary Tree 📘 Problem: Given an integer array without duplicates, the task is to build a maximum binary tree. The construction rules are: 1️⃣ The root is the maximum element in the array. 2️⃣ The left subtree is built recursively from elements to the left of the maximum. 3️⃣ The right subtree is built recursively from elements to the right of the maximum. Example: Input → [3,2,1,6,0,5] Output → [6,3,5,null,2,0,null,null,1] 🧠 My Approach: I solved this problem using a recursive divide-and-conquer approach. 1️⃣ Find the index of the maximum element in the given range — this becomes the root. 2️⃣ Recursively build the left subtree from the subarray before the maximum element. 3️⃣ Recursively build the right subtree from the subarray after the maximum element. 💡 What I Learned: ✅ How recursion naturally fits into tree construction problems ✅ The concept of divide and conquer applied to array-based tree building ✅ How to translate problem definitions into direct recursive structure #LeetCode #Java #DSA #BinaryTree #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟏𝟏 𝐨𝐟 #50DaysOfDSA Problem: [𝟑𝟑𝟏𝟖. 𝐅𝐢𝐧𝐝 𝐗-𝐒𝐮𝐦 𝐨𝐟 𝐀𝐥𝐥 𝐊-𝐋𝐨𝐧𝐠 𝐒𝐮𝐛𝐚𝐫𝐫𝐚𝐲𝐬 𝐈] 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐋𝐢𝐧𝐤 : https://lnkd.in/gy47HgPN In this problem, we are given an array nums and two integers k and x. We need to find the 𝐗-𝐒𝐮𝐦 for every subarray of length k. How the X-Sum works: 1️⃣ Count how many times each element appears in the subarray. 2️⃣ Keep only the top x most frequent elements if two have the same frequency, the larger element is prioritized. 3️⃣ Sum up all occurrences of those selected elements to get the X-Sum. If the subarray has less than x distinct elements, just take the sum of all its elements. Key learning: This problem beautifully combines HashMap for frequency counting and PriorityQueue (Min Heap) for selecting the top x elements efficiently. It also strengthens understanding of sliding window logic and priority-based data selection in Java. Here’s a quick breakdown from my solution: Used HashMap to store frequency of elements in each subarray Created a custom Pair class and used PriorityQueue to sort by frequency (and element value when frequencies matched) Extracted top x and calculated their contribution to the subarray sum Every day of this challenge helps reinforce logical thinking and pattern recognition — step by step toward stronger DSA foundations! #50DaysOfDSA #LeetCode #Java #ProblemSolving #CodingChallenge #DSA #DataStructures #Algorithms #WomenInTech #KeepLearning #Consistency
To view or add a comment, sign in
-
-
🌿 Day 74 of #100DaysOfCode 🌿 💡 Problem: Binary Tree Preorder Traversal – LeetCode 🚀 Approach: Used a recursive traversal to explore nodes in the order Root → Left → Right. Preorder traversal is all about visiting the leader first — just like taking the initiative before exploring possibilities! 💫 📊 Complexity Analysis: Time Complexity: O(n)** — every node is visited once Space Complexity: O(n)** — due to recursion stack ✅ Runtime: 0 ms (⚡ Beats 100%) ✅ Memory: 43.06 MB 🔑 Key Insight: Recursion helps untangle even the deepest branches — one root call at a time 🌱 #LeetCode #100DaysOfCode #BinaryTree #Recursion #CodingJourney #DSA #ProblemSolving #Java #Algorithms #ProgrammerLife #WomenInTech #CodeEveryday
To view or add a comment, sign in
-
-
✨ Day 103 of My DSA Challenge – Permutations 🔷 Problem : 46. Permutations 🔷 Goal : Generate all possible orderings (permutations) of a given array of distinct integers. Example → Input: [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 🔷 Key Insight : This problem is a classic recursion and backtracking pattern — exploring all possible arrangements by making choices and undoing them. Here’s how I approached it : 1️⃣ Recursion (Depth-First Search): Build each permutation step by step. 2️⃣ Visited Array: Track which elements have been used so far. 3️⃣ Backtracking: After exploring a path, undo the choice (remove the last element and mark it unvisited). Every recursive call expands the decision tree until all positions are filled, ensuring every unique ordering is generated. 🔷 My Java Approach : Recursive helper() function generates all permutations. Base case → when current list size equals array length. Used a boolean[] vis to manage state efficiently 🔷 Complexity : Time → O(n × n!) (since there are n! permutations and copying each list takes O(n)) Space → O(n) (for recursion and visited tracking) This problem beautifully tests recursion fundamentals, state management, and the art of backtracking — essential tools for mastering combinatorial problems. Each problem reminds me that problem-solving is less about memorizing patterns and more about learning how to think. #Day103 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Recursion #Backtracking #CodingChallenge #Programming #SoftwareEngineering #Algorithms #DataStructures #TechJourney #CodeEveryday #EngineerMindset #DeveloperJourney #GrowthMindset #KeepLearning #ApnaCollege #AlphaBatch #ShraddhaKhapra
To view or add a comment, sign in
-
-
💻 #Day486 (DSA) – LeetCode 3370: Smallest Number With All Set Bits 🔥 🚀 Problem Summary: Given a positive integer n, find the smallest number x ≥ n such that the binary representation of x contains only set bits (1s). 🧩 Examples: Input: n = 5 Output: 7 → Binary: 111 Input: n = 10 Output: 15 → Binary: 1111 Input: n = 3 Output: 3 → Binary: 11 💡 Intuition: To find the smallest number having all bits set, we can compute numbers like: 1 → (1), 3 → (11), 7 → (111), 15 → (1111), 31 → (11111)... and pick the smallest one ≥ n. 🧠 Approach: Keep generating (1 << k) - 1 until the result is ≥ n. That number will be our answer ✅ 🧩 Code (Java): class Solution { public int smallestNumber(int n) { int x = 1; while (x < n) { x = (x << 1) | 1; // shift and set next bit } return x; } } 🕒 Time Complexity: O(log n) 💾 Space Complexity: O(1) ✨ Key Learning: Mastery in bit manipulation 🧠 Smart usage of left shift and bitwise OR Understanding how binary patterns grow 🏷️ Tags: #LeetCode #DSA #BitManipulation #CodingChallenge #ProblemSolving #Java #DeveloperJourney #Algorithms #KeepLearning #CodeEveryday #TechCommunity 💬 Question for you: If you had to generalize this for the next “all-set bit number” above any given n, how would you optimize it? 🤔
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