📘 DSA Journey — Day 37 Today’s focus: Binary Search with APIs / decision functions. Problems solved: • Guess Number Higher or Lower (LeetCode 374) • First Bad Version (LeetCode 278) Concepts used: • Binary Search • Decision-based search (using API feedback) • Search space reduction Key takeaway: Both problems are classic examples of binary search on answers using an external API. In Guess Number Higher or Lower, we use the guess() API: • If guess is too high → move left • If too low → move right • If correct → return mid In First Bad Version, we use the isBadVersion() API: • If current version is bad → search left to find the first occurrence • Else → search right The key idea is: We don’t know the exact value, but we can narrow down the search space based on feedback. This pattern is useful in problems where: • A condition is monotonic (false → true transition) • We need to find the first/last occurrence of a condition Time complexity remains O(log n). Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
Binary Search with APIs and Decision Functions
More Relevant Posts
-
📘 DSA Journey — Day 31 Today’s focus: Binary Search for boundaries and square roots. Problems solved: • Sqrt(x) (LeetCode 69) • Search Insert Position (LeetCode 35) Concepts used: • Binary Search • Search space reduction • Boundary conditions Key takeaway: In Sqrt(x), the goal is to find the integer square root. Using binary search, we search in the range [1, x] and check mid * mid against x to narrow down the answer. This avoids linear iteration and achieves O(log n) time. In Search Insert Position, we use binary search to find either: • The exact position of the target, or • The correct index where it should be inserted The key idea is that when the target is not found, the final position of the left pointer gives the correct insertion index. These problems highlight how binary search is not just for finding elements, but also for determining positions and boundaries efficiently. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 29 Today’s focus: Binary Search on answer space. Problem solved: • Find Peak Element (LeetCode 162) Concepts used: • Binary Search • Observing slope / trend • Search space reduction Key takeaway: The goal is to find a peak element (an element greater than its neighbors). Instead of checking all elements, we use binary search by observing the slope: • If nums[mid] < nums[mid + 1] → we are on an increasing slope, so a peak must exist on the right side • Else → we are on a decreasing slope, so a peak lies on the left side (including mid) By following this logic, we eliminate half of the search space each time and find a peak in O(log n) time. The key insight is: A peak is guaranteed to exist, and the direction of slope helps guide the search. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 47of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Mirror Distance of an Integer Problem Insight: Given a number, the task is to find the absolute difference between the original number and its reversed form. Approach: • Stored the original number in a temporary variable • Reversed the number using digit extraction (modulo and division) • Calculated the absolute difference between the original and reversed number Time Complexity: O(d), where d = number of digits Space Complexity: O(1) Key Learnings: • Digit manipulation using modulo and division is a powerful technique • Always store the original value before modifying the input • Reversing numbers is a fundamental pattern in many DSA problems Takeaway: Breaking the problem into simple steps makes even tricky-looking logic easy to solve. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟖𝟏/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐗𝐎𝐑 𝐀𝐟𝐭𝐞𝐫 𝐑𝐚𝐧𝐠𝐞 𝐌𝐮𝐥𝐭𝐢𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧 𝐐𝐮𝐞𝐫𝐢𝐞𝐬 𝐈𝐈 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Group queries based on step size k. For small k, use prefix multiplication (range product trick) to apply updates efficiently. For large k, process directly since updates are fewer. Maintain a multiplier array and apply all updates at the end. Finally compute XOR of the transformed array. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Sqrt decomposition + prefix product + modular inverse + hashing. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ~𝐎(𝐧 √𝐧 + 𝐪 √𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: When operations vary by step size, splitting into small and large cases (sqrt decomposition) can drastically optimize performance. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Optimization #Math #PrefixSum #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
🔥 Day 107 DSA Problem Solving LeetCode 21 – Merge Two Sorted Lists 🔥 Today I solved one of the most classic linked list problems — “Merge Two Sorted Lists”. At first glance, it looks simple, but it tests how well you understand: Pointers in Linked List Iterative traversal Edge case handling (null nodes) Clean pointer manipulation without extra space 💡 Approach Used: I used a two-pointer technique with a dummy node to efficiently merge both sorted lists while maintaining order. Instead of creating new nodes, I focused on: 👉 Re-linking existing nodes 👉 Maintaining a tail pointer 👉 Handling remaining nodes carefully ⚙️ Time Complexity: O(n + m) ⚙️ Space Complexity: O(1) 🚀 Key Learning: Small pointer mistakes can break the whole structure — but once logic is clear, linked lists become powerful and elegant. This problem strengthened my fundamentals in linked list manipulation and boosted my confidence for more complex problems. #DSA #LinkedList #LeetCode #ProblemSolving #CodingJourney #Java
To view or add a comment, sign in
-
-
⭐Day 34 of #100DaysOfCode⭐ 🚀 Leveling Up: Solving the "Cheapest Flights Within K Stops" Challenge 🔍 Behind the Logic: My Approach In my Java implementation, I used a Breadth-First Search (BFS) approach to navigate the flight network efficiently. ✨ Here is a breakdown of the steps I took: 1️⃣Adjacency List Construction: I first transformed the flight data into an adjacency list to allow for quick lookups of neighboring cities and their respective costs. 2️⃣Level-Order Traversal (BFS): I utilized a Queue to explore the paths. Since we have a stop limit K, BFS is ideal because it allows us to process the graph level by level (stop by stop). 3️⃣Cost Optimization: I maintained a minCost array to track the cheapest way to reach each city. I only added a flight to my queue if the new path offered a lower cost than a previously recorded one. ⭐Constraint Management: By keeping track of the number of levels (stops) processed, I ensured the algorithm stopped as soon as we exceeded K stops, keeping the solution both accurate and efficient. 💯If you're stuck on a problem today, remember: The "Accepted" status is just the destination, but the true growth happens in the debugging. Keep coding, keep learning! 🚀 #LeetCode #Java #DataStructures #Algorithms #ProblemSolving #SoftwareEngineering #CodingJourney #BFS Anchal Sharma Ikshit ..
To view or add a comment, sign in
-
-
🚀 DSA Every Day – Day 2 📍 Problem: Minimum Distance to Target Element 💡 Platform: LeetCode 🧠 Approach: Brute Force with Bidirectional Traversal 🔍 Problem Summary: Given an array, a target value, and a starting index, find the minimum distance between the start index and any index where the target exists. ⚙️ Approach Explained: Traverse forward (start → end) Traverse backward (start → beginning) For each occurrence of target: Compute distance → |start - i| Maintain the minimum distance 📈 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) 🎯 Key Takeaways: Always think in terms of distance minimization problems Bidirectional traversal ensures complete coverage Can be optimized further using early stopping if needed Tell me your approach in comments ? Would love if people would like to join me in this habit for learning every day. 🔥 Progress: Day 2/ 60 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #SoftwareEngineering #60DaysOfCode
To view or add a comment, sign in
-
-
💡 LeetCode 191 — Number of 1 Bits (Hamming Weight) Recently solved an interesting bit manipulation problem that highlights the power of low-level optimization 🚀 🔍 Problem Statement: Given an unsigned integer, count the number of set bits (1s) in its binary representation. 🧠 Key Insight: Instead of checking every bit individually, we can use a clever trick: 👉 n & (n - 1) removes the rightmost set bit in each operation. This allows us to count only the set bits, making the solution more efficient. ⚙️ Approach Used (Brian Kernighan’s Algorithm): Initialize a counter Repeatedly apply n = n & (n - 1) Increment count until n becomes 0 📈 Time Complexity: O(k), where k = number of set bits (faster than checking all 32 bits) 📦 Space Complexity: O(1) ✨ Why this problem is important: Strengthens understanding of bit manipulation Frequently asked in interviews Useful in low-level optimization and system design 💬 Takeaway: Sometimes the best solutions come from understanding how data is represented at the binary level. Small tricks can lead to big optimizations! #LeetCode #DSA #CodingInterview #Java #BitManipulation #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 49 🔍 Solved: Largest Number Today I worked on an interesting problem where the goal was to arrange numbers such that they form the largest possible number. 💡 Key Insight: Instead of normal sorting, we compare numbers based on their string combinations (like "ab" vs "ba"). 📌 Approach: ✔ Converted integers to strings ✔ Used a custom comparator for sorting ✔ Compared (a + b) and (b + a) ✔ Sorted in descending order based on best combination ✔ Handled edge case when all elements are 0 Why this works: By comparing two numbers in different orders, we ensure that the arrangement always produces the maximum possible value when concatenated. 🎯 What I Learned: This problem taught me that sorting can go beyond numbers—custom logic and string manipulation are powerful tools in problem solving. #Java #DSA #LeetCode #Sorting #Comparator #CodingJourney #ProblemSolving #TechSkills 🚀
To view or add a comment, sign in
-
-
Today I solved: Group Anagrams 💡 Key Takeaway: The real trick was not comparing strings directly, but transforming each string into a standard form (sorted or frequency-based) and using it as a key for grouping. 👉 Approaches explored: - Sorting each string and using it as a key - Optimizing further using character frequency (better time complexity) 📊 Complexity: - Sorting approach → O(n * k log k) - Optimized approach → O(n * k) 🔍 What I learned: Many problems become easier when we change how we represent the data instead of solving them directly. Small shift in thinking → big impact in performance 💪 #DSA #LeetCode #Java #CodingJourney #SoftwareEngineering
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