⚡ 𝗗𝗮𝘆 𝟴𝟳 𝗼𝗳 𝗠𝘆 𝟭𝟬𝟬 𝗗𝗮𝘆𝘀 𝗼𝗳 𝗗𝗦𝗔 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲! 𝘛𝘰𝘥𝘢𝘺’𝘴 𝘱𝘳𝘰𝘣𝘭𝘦𝘮 𝘸𝘢𝘴 𝘢 𝘨𝘳𝘦𝘢𝘵 𝘳𝘦𝘮𝘪𝘯𝘥𝘦𝘳 𝘵𝘩𝘢𝘵 𝘤𝘰𝘯𝘴𝘵𝘳𝘢𝘪𝘯𝘵𝘴 𝘰𝘧𝘵𝘦𝘯 𝘥𝘦𝘧𝘪𝘯𝘦 𝘵𝘩𝘦 𝘳𝘦𝘢𝘭 𝘤𝘩𝘢𝘭𝘭𝘦𝘯𝘨𝘦. 𝙏𝙝𝙞𝙣𝙠𝙞𝙣𝙜 𝙗𝙚𝙮𝙤𝙣𝙙 𝙗𝙪𝙞𝙡𝙩-𝙞𝙣𝙨! 📌 Problem Solved: 1️⃣ 𝗟𝗲𝗲𝘁𝗰𝗼𝗱𝗲 𝟯𝟲𝟳: 𝗩𝗮𝗹𝗶𝗱 𝗣𝗲𝗿𝗳𝗲𝗰𝘁 𝗦𝗾𝘂𝗮𝗿𝗲 (𝗘𝗮𝘀𝘆) ➡️ Without using built-in functions like sqrt(). ✨ Key Learnings: 🔹 Since using 𝘀𝗾𝗿𝘁() 𝘄𝗮𝘀 𝗻𝗼𝘁 𝗮𝗹𝗹𝗼𝘄𝗲𝗱, the solution required logical thinking instead of shortcuts. 🔹 A clean approach is using 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 between 1 and num, checking whether mid * mid == num. 🔹 This reduces 𝘁𝗶𝗺𝗲 𝗰𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 to: ✅ 𝗢(𝗹𝗼𝗴 𝗻) 𝗶𝗻𝘀𝘁𝗲𝗮𝗱 𝗼𝗳 𝗢(𝗻) 🔹 Important to 𝗵𝗮𝗻𝗱𝗹𝗲 𝗶𝗻𝘁𝗲𝗴𝗲𝗿 𝗼𝘃𝗲𝗿𝗳𝗹𝗼𝘄 while calculating mid * mid (use long if needed). 🧠 Big Takeaway: When shortcuts are removed, 𝗮𝗹𝗴𝗼𝗿𝗶𝘁𝗵𝗺𝗶𝗰 𝘁𝗵𝗶𝗻𝗸𝗶𝗻𝗴 takes over. 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 isn’t just for arrays — it applies anywhere there’s a 𝘀𝗲𝗮𝗿𝗰𝗵 𝘀𝗽𝗮𝗰𝗲. Day 87 completed — 𝘁𝗵𝗶𝗻𝗸𝗶𝗻𝗴 𝗯𝗲𝘆𝗼𝗻𝗱 𝗯𝘂𝗶𝗹𝘁-𝗶𝗻𝘀! 💪🔥 #100DaysOfCode #DSA #Java #LeetCode #BinarySearch #ProblemSolving #InterviewPreparation #LearningInPublic #Developers #MathLogic #ProblemSolving #NumberTheory #InterviewPreparation #LearningInPublic #Developers
Java Day 87: Binary Search Solution for LeetCode Challenge
More Relevant Posts
-
🔥 Day 98 of #100DaysOfCode Today’s challenge: LeetCode – Search in Rotated Sorted Array II 🔄🔍 📌 Problem Summary You are given a rotated sorted array that may contain duplicates. Your task is to determine whether a target exists in the array. Example: Input: nums = [2,5,6,0,0,1,2], target = 0 Output: true 🧠 Approach Used: Linear Search In this implementation, we simply iterate through the array and check if the element matches the target. ⚙️ Logic for(int i = 0; i < nums.length; i++){ if(nums[i] == target){ return true; } } return false; 💡 Explanation Traverse the array from start to end If the target is found → return true If the loop finishes → return false ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🚀 Performance Runtime: 0 ms Memory: 44.9 MB 🧠 Learning This problem is a variation of Search in Rotated Sorted Array, but with duplicates allowed. While a Binary Search solution exists, duplicates can break the strict ordering and make the logic more complex. A simple linear scan ensures correctness in all cases. Only 2 days left to complete #100DaysOfCode 🚀 Almost at the finish line! On to Day 99 🔥 #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode Solved 154. Find Minimum in Rotated Sorted Array II on LeetCode 🔍 🧠 Key Insight: The array is sorted but rotated, and this version introduces duplicates, which makes the binary search logic slightly trickier. ⚙️ Approach: 🔹Use binary search with left and right pointers 🔹Compare nums[mid] with nums[right]: 🔹If nums[mid] > nums[right] → minimum lies in the right half 🔹If nums[mid] < nums[right] → minimum lies in the left half (including mid) 🔹If equal → safely shrink the search space by decrementing right This handles the ambiguity caused by duplicates. ⏱️ Time Complexity: Average: O(log n) Worst case: O(n) (when many duplicates exist) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
#100DaysOfCode 👉 LeetCode #374 – Guess Number Higher or Lower This problem reinforces how Binary Search works even when the comparison logic is hidden behind an API. 🔹 Problem Insight: We don’t compare numbers directly. Instead, we rely on the `guess()` API which tells: • -1 → guessed number is too high • 1 → guessed number is too low • 0 → correct guess That still creates a sorted search space → perfect for Binary Search. 🔹 Idea: Applied Binary Search between 1 and n. • If guess(mid) == 0 → found the answer • If guess(mid) == -1 → move left • If guess(mid) == 1 → move right ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) ✔ Practiced API-based binary search ✔ Improved decision-making using feedback instead of direct comparison ✔ Strengthened core Binary Search intuition 💡 Takeaway: Even when values are hidden, Binary Search shines as long as the feedback is monotonic. #Java #DSA #BinarySearch #LeetCode #ProblemSolving #LearnInPublic #SoftwareEngineering #CodingJourney
To view or add a comment, sign in
-
-
🚀 𝗝𝗮𝘃𝗮 𝗕𝗲𝗴𝗶𝗻𝗻𝗲𝗿 𝗦𝗲𝗿𝗶𝗲𝘀 | 𝗣𝗮𝗿𝘁 𝟱 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀, 𝗦𝗰𝗼𝗽𝗲 & 𝗟𝗶𝗳𝗲𝘁𝗶𝗺𝗲 Variables are not just “containers.” They define where data lives, how long it lives, and who can access it. Let’s break it down clearly: 🔹 𝗟𝗼𝗰𝗮𝗹 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 Declared inside methods. Stored in 𝙎𝙩𝙖𝙘𝙠 𝙢𝙚𝙢𝙤𝙧𝙮. They exist only while the method runs. Once the method finishes → memory is cleared. That’s why they “disappear.” 🔹 𝗜𝗻𝘀𝘁𝗮𝗻𝗰𝗲 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 Declared inside a class, outside methods. Stored in 𝙃𝙚𝙖𝙥 𝙢𝙚𝙢𝙤𝙧𝙮 (inside objects). They live as long as the object exists. No object → no instance variables. 🔹 𝗦𝘁𝗮𝘁𝗶𝗰 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 Declared using static. Stored in the 𝙈𝙚𝙩𝙝𝙤𝙙 𝘼𝙧𝙚𝙖 (Class memory). Created only once when the class loads. Shared across all objects of that class. 💡 𝙏𝙝𝙚 𝙧𝙚𝙖𝙡 𝙙𝙞𝙛𝙛𝙚𝙧𝙚𝙣𝙘𝙚? 𝗟𝗼𝗰𝗮𝗹 → 𝘔𝘦𝘵𝘩𝘰𝘥-𝘭𝘦𝘷𝘦𝘭 𝘭𝘪𝘧𝘦 𝗜𝗻𝘀𝘁𝗮𝗻𝗰𝗲 → 𝘖𝘣𝘫𝘦𝘤𝘵-𝘭𝘦𝘷𝘦𝘭 𝘭𝘪𝘧𝘦 𝗦𝘁𝗮𝘁𝗶𝗰 → 𝘊𝘭𝘢𝘴𝘴-𝘭𝘦𝘷𝘦𝘭 𝘭𝘪𝘧𝘦 When you understand scope & lifetime, you stop memorizing… and start thinking like the JVM. Hashtags : #Java #JavaBeginnerSeries #JavaMemory #StackVsHeap #BackendDevelopment #Programming #SoftwareEngineering #LearnInPublic #CodingJourney #Developers #Backend #API #Developers
To view or add a comment, sign in
-
-
🔥 Day 97 of #100DaysOfCode Today’s problem: LeetCode – Search in Rotated Sorted Array 🔄🔍 📌 Problem Summary You are given a rotated sorted array and a target. Your task is to return the index of the target if it exists; otherwise return -1. Example: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 🧠 Approach Used: Linear Search In this solution, we simply iterate through the array and check if the current element equals the target. ⚙️ Logic for(int i = 0; i < nums.length; i++){ if(nums[i] == target){ return i; } } return -1; 💡 Explanation Traverse the array from start to end If the target is found → return the index If the loop finishes → return -1 ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🚀 Performance Runtime: 0 ms Memory: 43.68 MB 🧠 Learning This problem can also be solved using Binary Search in O(log n) by identifying which half of the rotated array is sorted. But for smaller inputs or quick validation, linear scan works correctly and is simple to implement. Only 3 days left to complete the #100DaysOfCode challenge 🚀 Consistency is the real win here! On to Day 98 🔥 #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
Today’s challenge was all about logic and boundary management. Solving the Spiral Matrix in Java isn't just about the code; it’s about visualizing how to traverse a 2D array while keeping track of four changing boundaries (top, bottom, left, right). It’s easy to get lost in the indices, but once the logic clicks, it feels like magic. These are the kinds of problems that sharpen the mind for the rigorous interviews at companies like Google and Microsoft. One step closer to the goal! 🚀 #DSA #Java #CodingJourney #SpiralMatrix #ProblemSolving #SoftwareEngineer #DataStructures #Algorithms
To view or add a comment, sign in
-
-
🚀Day - 24 | Day - 8: Deep Dive into Linked Lists! Today, my DSA journey took me beyond the basics of Arrays and into the flexible world of Linked Lists. While Arrays are great, I quickly realized they have their limits—specifically when it comes to memory and efficiency. 📉 The "Array" Problem Arrays require contiguous memory, which can be a pain to allocate for large datasets. Plus, inserting or deleting elements is expensive because you have to shift everything else around. 💡 The Solution: Linked Lists A Linked List is a collection of Nodes. Each node is like a small container with two parts: Data: The actual value you want to store. Next (Address): A pointer/reference to the next node in the sequence. The list starts with a Head and ends when a node points to Null. No more shifting elements! Just update the pointers, and you're good to go. 🛠️ What I Built Today I moved from theory to implementation using Java. Here’s a snapshot of what I practiced: Structure: Defined the Node using Classes and Objects. Traversal: Mastered the while loop to navigate the list (since we don't have indexes like Arrays). Core Operations: Adding elements (at the beginning, end, and specific indexes). Removing elements (first and last). Printing the entire list. 🧠 Key Takeaway If you need fast insertions and deletions without worrying about pre-allocating memory blocks, Linked Lists are best. A big thanks to Poovizhi Mam for the clear explanation and hands-on coding practice✨ #DSA #CodingJourney #Java #DataStructures #LinkedList #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
After exploring path-based recursion, I moved to generating all subsets of a string. This introduced a very clean pattern - For every element, you either include it or exclude it. That’s it. But that “either-or” creates a full decision tree. What became clear here: - Every element creates two branches. - Total subsets = 2ⁿ (tree expansion becomes visible). - Recursion can systematically explore combinations. - The structure is more important than the syntax. Core logic : if (index == str.length()) { System.out.println(current); return; } subset(index + 1, current + str.charAt(index)); // include subset(index + 1, current); // exclude This is where recursion started feeling predictable. Once the pattern is seen, similar problems start looking connected. #recursion #java #dsajourney #backtracking #problemSolving
To view or add a comment, sign in
-
✅ Solution — LeetCode 739. Daily Temperatures (Java) 🧠 Idea (Monotonic Stack) We use a stack to store indices of days. The stack keeps temperatures in decreasing order. When we find a warmer temperature, we: Pop the previous index from stack Calculate the difference in days Store it in the result array 💻 Java Code class Solution { public int[] dailyTemperatures(int[] temperatures) { int n = temperatures.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for(int i = 0; i < n; i++){ while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){ int prev = stack.pop(); res[prev] = i - prev; } stack.push(i); } return res; } } ⏱ Time Complexity O(n) Each element is pushed and popped once. 💾 Space Complexity O(n) 🚀 100 Days of Learning & Problem Solving – Day 79 🚀 📅 Day 79 / 100 Continuing my #100DaysOfLeetCode journey by exploring the Monotonic Stack pattern today. ✅ Today’s Progress: 🔹 Solved LeetCode 739 – Daily Temperatures 🔹 Implemented the monotonic stack technique for efficient comparisons 🔹 Practiced solving “next greater element” type problems 🔹 Improved understanding of stack-based problem-solving patterns 🧠 Key Learnings: ✔️ Using stacks to track unresolved elements ✔️ Identifying the “next greater element” pattern ✔️ Avoiding brute-force nested loops with a linear-time solution ✔️ Writing efficient solutions with better time complexity Problems like this show how the right data structure can reduce complexity dramatically. From nested loops to linear time — the power of patterns 🚀 📌 Alongside DSA, I continue focusing on: ✔️ Strengthening core programming fundamentals ✔️ Recognizing reusable problem-solving patterns ✔️ Writing clean and efficient code ✔️ Staying consistent with daily practice Learning every day, improving one problem at a time 💪 #100DaysOfLearning #LeetCode #DSA #Java #ProblemSolving #CodingJourney #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟐𝟓 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on sorting an array containing only three distinct values using an efficient counting approach. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Sort Colors 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝟏 – 𝐂𝐨𝐮𝐧𝐭𝐢𝐧𝐠 𝐀𝐫𝐫𝐚𝐲 • Created a count array of size 3 • Counted occurrences of 0, 1, and 2 • Overwrote the original array using the frequency values • Simple and easy to implement 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • When value range is small, counting is powerful • Rewriting the array can be simpler than swapping • Constraints often guide the optimal approach • Clear thinking avoids overcomplication 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) (since count array size is constant) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Not every sorting problem needs full sorting logic — sometimes counting is enough. 25 days consistent. On to Day 26 🚀 #DSA #Arrays #Sorting #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
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