Day 47/100 – #100DaysOfCode 🚀 | #Java #Arrays #InPlaceAlgorithms ✅ Problem Solved: First Missing Positive (LeetCode 41) 🧩 Problem Summary: Given an unsorted array, find the smallest missing positive integer. The solution must run in O(n) time and constant extra space. 💡 Approach Used: This is not a direct sorting or hashing problem — the key is index positioning. Logic: The answer must lie within 1 to n+1. Place each positive number x at index x-1 if possible. Then scan to find the first index where nums[i] != i+1. Steps: Iterate and swap values into their correct position. Ignore values ≤0 or >n. Final scan to find the first missing positive. ⚙️ Time Complexity: O(n) 📦 Space Complexity: O(1) ✨ Takeaway: This problem teaches how index-based hashing can eliminate the need for extra space — very common in interview-style optimization problems. #Java #LeetCode #Algorithm #Array #ProblemSolving #100DaysOfCode #CodingChallenge
Solved LeetCode 41: First Missing Positive in O(n) time
More Relevant Posts
-
Day 31/100 – #100DaysOfCode 🚀 | #Java #LeetCode #Sorting #Arrays ✅ Problem Solved: Maximum Gap ⚙️ 🧩 Problem Summary: Given an unsorted array, find the maximum difference between successive elements in its sorted form. You must solve it in linear time and space. 💡 Approach Used: Used Bucket Sort (Pigeonhole Principle) to achieve linear performance — ➤ Calculated global min & max. ➤ Divided range into buckets based on array size. ➤ Placed elements into buckets tracking min & max of each. ➤ Found maximum gap between successive non-empty buckets. ⚙️ Time Complexity: O(N) 📦 Space Complexity: O(N) ✨ Takeaway: This problem sharpened my understanding of bucket-based approaches and how sorting principles can be applied efficiently without direct sorting. ⚡ #Java #LeetCode #BucketSort #Sorting #ProblemSolving #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 17 of #JavaWithDSAChallenge - Remove Duplicates from Sorted Array Today’s DSA problem was a classic but super-important one - removing duplicates from a sorted array in-place. This problem teaches how to think in terms of two-pointer techniques, in-place modifications, and optimizations without extra space-all of which are heavily used in interviews at top product-based companies. Problem Summary We are given a sorted integer array, and the challenge is to: Remove duplicate values Keep one copy of each element Do it in-place (no extra array allowed) Return the count of unique elements Anything beyond that index doesn’t matter. Approach Used - Two Pointer Technique Since the array is already sorted, duplicates appear next to each other. We use: Pointer j → scans all elements Pointer i → stores position where next unique number should go Whenever we find a number that is different from the previous, we copy it to nums[i] and move i forward. This ensures all unique elements accumulate at the front of the array. Java Code class Solution { public int removeDuplicates(int[] nums) { int i = 1; // pointer for unique elements for (int j = 1; j < nums.length; j++) { if (nums[j] != nums[j - 1]) { nums[i] = nums[j]; i++; } } return i; // number of unique elements } } Key Takeaways Sorted arrays make duplicate detection easy Two-pointer pattern is extremely powerful In-place operations improve space efficiency Great practice for interview questions on arrays & optimization #JavaWithDSAChallenge #Day17 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #TwoPointerTechnique #WomenInTech #SoftwareEngineering #LearnToCode #CodeNewbie
To view or add a comment, sign in
-
-
Day 28/100 – #100DaysOfCode 🚀 | #Java #LeetCode #DynamicProgramming ✅ Problem Solved: Scramble String 🔀 🧩 Problem Summary: Given two strings s1 and s2, determine whether one is a scrambled version of the other. A string is scrambled by recursively dividing it into two non-empty substrings and swapping them. 💡 Approach Used: Implemented Recursion + Memoization using a HashMap for overlapping subproblems. Used character frequency checks to prune unnecessary recursion calls. Used Java’s BiFunction with inline helper logic for recursion. ⚙️ Time Complexity: O(n⁴) (due to substring operations and recursion) 📦 Space Complexity: O(n²) ✨ Takeaway: Even complex recursive problems can be optimized efficiently with Memoization and early pruning. 🚀 #Java #LeetCode #DynamicProgramming #Recursion #ProblemSolving #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
Day 80 of #100DaysOfCode Solved Frequency Sort in Java 🔠 Approach Today's problem was to sort an array based on the frequency of its numbers. My initial solution was accepted, but it exposed a major efficiency gap! My strategy involved: Sorting the array first. Using nested loops to count the frequency of each number and store it in a HashMap. (Implied) Using the counts to perform the final custom sort. The core issue was the counting method: running a full loop inside another full loop to get the frequency. This quadratic $O(N^2)$ counting completely tanked the performance. ✅ Runtime: 29 ms (Beats 12.02%) ✅ Memory: 45.26 MB (Beats 5.86%) Another great lesson in algorithmic complexity! The difference between $O(N^2)$ and the optimal $O(N \log N)$ for this problem is massive. Time to refactor and implement the fast, single-pass $O(N)$ frequency count using getOrDefault! 💪 #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #HashMap #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
📅 Day 81 of #100DaysOfLeetCode Problem: Insert into a Binary Search Tree (LeetCode #701) Approach: The task is to insert a new node with a given value into a Binary Search Tree (BST). Start from the root and recursively find the correct position: If the new value is smaller than the current node’s value, go to the left subtree. Otherwise, go to the right subtree. When a null spot is found, insert a new node there. The BST property is preserved throughout this process. Complexity: ⏱️ Time: O(h) — where h is the height of the tree. 💾 Space: O(h) — recursive call stack. 🔗 Problem Link: https://lnkd.in/dCS7zxVG 🔗 Solution Link: https://lnkd.in/dxB4ZNtV #LeetCode #100DaysOfCode #BinarySearchTree #Recursion #Java #TreeTraversal #DSA #Algorithms #CodingChallenge #ProblemSolving #CodeNewbie #StudyWithMe #BuildInPublic #LearnToCode #DailyCoding
To view or add a comment, sign in
-
-
🔥 LeetCode Day--- 4 | “Median of Two Sorted Arrays” (Hard, Java) Today’s challenge was one of those that really test your logic, patience, and understanding of binary search. This problem wasn’t about just merging two sorted arrays — it was about thinking smarter 🧠. Instead of brute-forcing through both arrays (O(m+n)), I implemented a binary partition approach to achieve O(log(min(m, n))) efficiency 💡 What I learned today: Always choose the smaller array for binary search — it makes the partition logic simpler. Handle boundaries carefully with Integer.MIN_VALUE and Integer.MAX_VALUE. The goal is to find the perfect partition where: Left half ≤ Right half Elements are balanced across both arrays Once that’s done → median can be easily calculated! ✅ Result: Accepted | Runtime: 0 ms 🚀 Hard problem turned into a logic puzzle that was actually fun to solve! 🧩 Concepts Strengthened: Binary Search Partitioning Logic Edge Case Handling Mathematical Thinking #LeetCode #Day4 #Java #BinarySearch #ProblemSolving #CodingChallenge #DataStructures #Algorithms #CodeEveryday #DeveloperJourney #TechLearning #LeetCodeHard #CodingCommunity
To view or add a comment, sign in
-
-
🚀 Day 42 of #100DaysOfLeetCode Today's challenge: Binary Tree Preorder Traversal (LeetCode #144) 🌳 📘 Concept: Preorder traversal follows the order — Root → Left → Right. It’s one of the fundamental ways to explore a binary tree, and it helps build a strong base for understanding recursion and tree-based algorithms. 🧠 Approach Used: Used a simple recursive solution to visit the root first, then the left subtree, and finally the right subtree. 💡 Key Takeaway: Recursion becomes easy once you visualize how the function calls go deep into left and right subtrees. Trees are all about patterns! #LeetCode #100DaysChallenge #Day42 #CodingJourney #BinaryTree #Recursion #Java
To view or add a comment, sign in
-
-
🚀 Day 42 of #100DaysOfLeetCode Today's challenge: Binary Tree Preorder Traversal (LeetCode #144) 🌳 📘 Concept: Preorder traversal follows the order — Root → Left → Right. It’s one of the fundamental ways to explore a binary tree, and it helps build a strong base for understanding recursion and tree-based algorithms. 🧠 Approach Used: Used a simple recursive solution to visit the root first, then the left subtree, and finally the right subtree. 💡 Key Takeaway: Recursion becomes easy once you visualize how the function calls go deep into left and right subtrees. Trees are all about patterns! #LeetCode #100DaysChallenge #Day42 #CodingJourney #BinaryTree #Recursion #Java
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