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
Solved Maximum Gap problem using Bucket Sort in Java
More Relevant Posts
-
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
To view or add a comment, sign in
-
-
Day 45/100 – #100DaysOfCode 🚀 | #Java #Hashing #SlidingWindow ✅ Problem Solved: Contains Duplicate III (LeetCode 220) 🧩 Problem Summary: Given an integer array and integers k and t, determine if there exist two distinct indices i and j such that: |i - j| ≤ k |nums[i] - nums[j]| ≤ t 💡 Approach Used: Used a Sliding Window + TreeSet to maintain numbers in a sorted structure. Steps: Traverse the array and maintain a window of at most size k. For each element x, find if there exists another element in the set such that |x - y| ≤ t. This is done using ceiling() to find the closest value ≥ x. Insert the element in TreeSet and remove the element that slides out. ⚙️ Time Complexity: O(n log k) 📦 Space Complexity: O(k) ✨ Takeaway: This problem teaches how ordered data structures like TreeSet help efficiently handle range queries in sliding window scenarios. #Java #TreeSet #SlidingWindow #LeetCode #ProblemSolving #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
🚀 LeetCode #58 – Length of Last Word (Java Solution) Today I solved a classic string problem that tests how well you handle edge cases and string traversal logic. 🧩 Problem Statement: Given a string s consisting of words and spaces, return the length of the last word in the string. A word is defined as a maximal substring consisting of non-space characters only. 🧠 Dry Run Example Input: " fly me to the moon " ➡ After skipping spaces → "fly me to the moon" ➡ Last word = "moon" ➡ Output: 4 ⚙️ Time & Space Complexity Time Complexity: O(n) — we traverse the string once (from the end to the start). Space Complexity: O(1) — no extra space used apart from a few variables. 💡 Key Takeaways: ✅ Learned how to handle trailing spaces properly before counting. ✅ Strengthened understanding of string traversal in reverse. ✅ Explored an alternative approach using trim() + split("\\s+") for readability. ✅ Remembered to test with edge cases like "a" or "Hello " to ensure correctness. Every small problem like this helps in building stronger fundamentals 💪 #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #75DaysOfCode #DataStructures #Algorithms
To view or add a comment, sign in
-
-
🌳 Day 65 of #LeetCode Journey 🔹 Problem: 106. Construct Binary Tree from Inorder and Postorder Traversal 🔹 Difficulty: Medium 🔹 Language: Java Today’s problem is about rebuilding a binary tree when you’re given its inorder and postorder traversal arrays. 🧩 Key Idea: The last element in postorder is always the root. In inorder traversal, elements to the left of the root are in the left subtree, and elements to the right are in the right subtree. We recursively build the tree using this property. 💡 Approach: Start from the end of the postorder array to get the root. Use a hashmap to store inorder indices for O(1) lookup. Recursively construct the right subtree first, then the left subtree (since we’re moving backward in postorder). 🧠 Concepts reinforced: Recursion HashMap for index lookup Understanding inorder & postorder relationships 🔥 Another step forward in mastering tree construction problems! #LeetCode #100DaysOfCode #Java #CodingChallenge #DataStructures #BinaryTree #Recursion #ProblemSolving #ProgrammingJourney
To view or add a comment, sign in
-
-
Day 50/100 – #100DaysOfCode 🚀 | #Java #Arrays #GreedyApproach ✅ Problem Solved: Minimum Operations to Convert All Elements to Zero (LeetCode) 🧩 Problem Summary: You’re given an array of integers. In one operation, you can subtract the smallest non-zero value from every non-zero element. Return the number of operations needed to make all elements zero. 💡 Approach Used: Instead of simulating operations (which would be inefficient), observe: Each distinct positive value in the array contributes exactly one operation. Because removing the smallest non-zero repeatedly is equivalent to counting how many unique positive values exist. So, Answer = Count of unique positive elements Why this works: Each distinct positive number represents one “level” of reduction until zero. ⚙️ Time Complexity: O(N) 📦 Space Complexity: O(N) (for set) ✨ Takeaway: This problem highlights how pattern recognition and mathematical simplification often outperform brute-force logic. #Java #LeetCode #MathLogic #GreedyAlgorithm #ProblemSolving #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 67 Reverse Words in a String Problem: – Reverse Words in a String Task: – Given a string, reverse the order of the words and remove extra spaces. Example: Input: s = " the sky is blue " → Output: "blue is sky the" My Approach: Used trim() to remove leading and trailing spaces. Split the string using split("\\s+") to handle multiple spaces. Reversed the array and joined the words with a single space. Time Complexity: O(N) | Space Complexity: O(N) Even simple string problems can teach the importance of clean code and efficient use of built-in methods. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #GeeksForGeeks #CodeNewbie #StringManipulation
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 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 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 related topics
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