📅 Day 7 of My DSA Journey Today I solved the “Search Insert Position” problem on LeetCode using Java. This problem introduced me to the concept of Binary Search, one of the most important searching techniques in algorithms. 💡 Problem Overview Given a sorted array and a target value: If the target exists → return its index. If it does not exist → return the index where it should be inserted to keep the array sorted. Instead of checking every element (linear search), I used Binary Search to reduce the search space by half in every step. ⚙️ Approach I Used Defined two pointers: start (si) and end (ei) Calculated the middle index (mid) Compared nums[mid] with the target Adjusted the search range accordingly If the target was not found, returned the correct insert position This approach is efficient because it avoids unnecessary comparisons. ⚡ Challenges I Faced The tricky part was understanding what value to return when the element is not present in the array. I had to carefully track where the target should logically be inserted while adjusting the pointers. 🛠 How I Solved It I performed several dry runs and visualized how the pointers move in each iteration. This helped me understand how binary search naturally leads to the correct insertion index. 📊 Complexity Analysis Time Complexity: O(log n) Because Binary Search divides the array into halves each iteration. Space Complexity: O(1) Only a few variables are used, so no extra memory is required. 📚 Key Learnings Deep understanding of Binary Search How to handle edge cases when the target is not present Improved ability to analyze time and space complexity Every day I realize that DSA is not just about coding — it’s about thinking smarter and optimizing solutions. On to Day 8 tomorrow. The consistency continues. 💪 #DSA #100DaysOfCode #LeetCode #BinarySearch #ProblemSolving #CodingJourney #Java #LearningInPublic #Consistency #SoftwareDevelopment
Day 7: Solved Search Insert Position with Java and Binary Search
More Relevant Posts
-
🚀 Day 12 of My DSA Journey Today I solved the “Contains Duplicate” problem on LeetCode using Java. 💡 Problem Statement Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example: Input: [1,2,3,1] Output: true Because the value 1 appears more than once. ⚙️ Approach I Used I solved this problem using sorting. Steps: 1️⃣ First, I sorted the array using Arrays.sort() 2️⃣ Then I traversed the array once 3️⃣ Compared each element with the next element 4️⃣ If nums[i] == nums[i+1], it means a duplicate exists → return true 5️⃣ If no duplicates are found, return false 📊 Complexity Analysis Time Complexity: O(n log n) → due to sorting Space Complexity: O(1) (ignoring sorting internal space) 📚 Key Learning Even simple problems can be solved in multiple ways. Sorting helped reduce the problem to a simple adjacent comparison. In future, I’ll also explore a HashSet approach which can solve this in O(n) time. Another day of improving problem-solving skills and algorithmic thinking. On to Day 13 tomorrow. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #Algorithms #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Day 11 of My DSA Journey Today I solved the “Power of Two” problem on LeetCode using Java. The task is simple but interesting: Given an integer n, determine whether it is a power of two. A number is a power of two if it can be written as: n=2xn = 2^xn=2xfor some integer x. Example: Input: 1 → Output: true (2⁰ = 1) Input: 16 → Output: true (2⁴ = 16) Input: 3 → Output: false 💡 Approach I Used I iterated through possible powers of 2 and checked if any of them equals the given number. Steps: 1️⃣ Loop from 0 to 31 (since 2³¹ is near the integer limit). 2️⃣ Calculate 2^i using Math.pow(). 3️⃣ If n == 2^i, return true. 4️⃣ If no match is found, return false. 📊 Complexity Analysis Time Complexity: O(log n) Space Complexity: O(1) ⚡ Result ✅ Accepted ⚡ Runtime: 1 ms (Beats 96.25%) 📚 Key Learning Even simple problems help reinforce mathematical patterns and number properties, which are very useful in algorithm design. Tomorrow I’ll try to explore more optimized approaches like bit manipulation for this problem. On to Day 12. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #Algorithms #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 DAY 69/150 — FINDING PATTERNS THROUGH SORTING! 🚀 Day 69 of my 150 Days DSA Challenge in Java and today I solved a problem that highlights the power of sorting and pattern observation 💻🧠 📌 Problem Solved: Minimum Absolute Difference 📌 LeetCode: #1200 📌 Difficulty: Easy–Medium The task is to find all pairs of elements with the minimum absolute difference in a given array. 🔹 Approach Used Instead of comparing every pair (which would take O(n²)), I used a more optimized approach: • First, sort the array • Then check only adjacent elements • Keep track of the minimum difference • Store all pairs that match this minimum difference Sorting helps reduce unnecessary comparisons and simplifies the logic. ⏱ Complexity Time Complexity: O(n log n) (due to sorting) Space Complexity: O(n) (for storing result pairs) 🧠 What I Learned • Sorting can significantly reduce the complexity of comparison-based problems • Adjacent elements in a sorted array often reveal important patterns • Avoid brute-force when a structured approach exists 💡 Key Takeaway This problem taught me that sometimes the simplest optimization is just reordering the data first. Once sorted, many complex problems become easier to solve. It also strengthened my understanding of how pattern recognition + sorting can lead to efficient solutions. ✅ Day 69 completed 🚀 81 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/gYHCJmkv 💡 Sorting is not just a technique — it’s a problem-solving strategy. #DSAChallenge #Java #LeetCode #Sorting #Greedy #150DaysOfCode #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🔥 Turning logic into performance! 🔥 Just cracked the Two Sum problem using the Two Pointer approach in Java — and the results are super satisfying 👇 ⚡ Runtime: 2 ms (Beats 96.34%) ⚡ Memory: Optimized & efficient ⚡ Approach: Clean, scalable, and interview-ready 💡 What made the difference? Instead of going with brute force, I leveraged the power of pattern recognition and applied the two-pointer technique on a sorted array — reducing complexity and boosting performance 🚀 🧠 Key Takeaways: ✨ Patterns > Memorization ✨ Optimization matters ✨ Writing clean code is a superpower ✨ Every problem is an opportunity to think deeper 📈 Slowly but surely, leveling up my DSA skills and building a strong problem-solving mindset. Consistency + Curiosity = Growth 💯 Let’s keep pushing limits 💪 #Java #DSA #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode #Tech #Learning #Developers
To view or add a comment, sign in
-
-
🚀 DSA Learning Journey | Day 2 | Java Solved “Search in Rotated Sorted Array.” 💡 Key Idea: Applied Binary Search by determining which half of the array is sorted and narrowing the search space. ⚙ Implementation • Language: Java • Time Complexity: O(log n) • Space Complexity: O(1) 📚 Learning how binary search can be adapted to work with rotated sorted arrays. #Java #DSA #LeetCode #ProblemSolving #BinarySearch #JavaDeveloper
To view or add a comment, sign in
-
-
🚀 Day 1 of my DSA Journey with CodeOjas! Today, I solved a “Array Reversal in Blocks” problem using Java. Problem: Given an array of integers and a block size, reverse the array in blocks of the given size, but keep the middle block unchanged. Example: Input: arr = [1, 2, 3, 4, 5, 6, 7], blockSize = 3 Output: [3, 2, 1, 4, 5, 6, 7] Approach: Divided the array into blocks of the given size Reversed all blocks except the middle block Constructed a new array to store the modified result (brute-force approach) Complexity: Time Complexity: O(n) Space Complexity: O(n) This is a brute-force approach — simple, clear, and easy to understand for beginners. 💡 Takeaway: Breaking down a problem into blocks can help solve complex array manipulation problems easily. 📂 Code is available on my GitHub: [https://lnkd.in/g4yxAJs3] #DSA #Java #CodingJourney #CodeOjas #ProblemSolving #LinkedInLearning
To view or add a comment, sign in
-
🚀 Day 31 / 180 – DSA with Java 🚀 📘 Topic Covered: Strings & Substring Search 🧩 Problem Solved: Find the Index of the First Occurrence in a String Problem: Given two strings haystack and needle, return the index of the first occurrence of needle in haystack. If it is not present, return -1. Approach: Traversed the main string and checked potential starting positions where the first character matched. Then compared the substring of the same length as the target string to verify a complete match. Key Learning: ✔️ Understanding substring search logic ✔️ Optimizing comparisons by checking the first character first ✔️ Practicing careful boundary handling in string traversal If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #Strings #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 1 of Java with DSA Journey 📌 Topic: Binary Search (LeetCode 704) 💬 “Today I practiced a very fundamental problem from LeetCode.” Today was all about efficiency and smart problem solving. While Linear Search checks every element one by one, Binary Search drastically reduces the search space by half in each step — making it one of the most powerful techniques in DSA. ✨ What I Learned: 🔹 Divide & Conquer: Reducing the problem size at every step leads to faster solutions 🔹 Prerequisite: Works only on sorted arrays 🔹 Implementation: Used iterative approach with two pointers (low & high) 🔹 Time Complexity: O(log n) | Space Complexity: O(1) 🔹 Common Mistake: Wrong mid calculation or improper pointer updates causing infinite loops 🔹 Real-World Use: Search engines, databases, efficient lookup systems 🔹 Optimization Insight: Much faster than Linear Search (O(n)) for large datasets 💡 Pro Tip (Java Developers): Always calculate mid like this: mid = low + (high - low) / 2; 👉 Prevents integer overflow and makes your code production-ready. 🧠 Performance Insight: ✔️ Linear Search: If you have 1 million elements, you might check 1 million times. ✔️ Binary Search: For that same 1 million elements, you only need 20 checks max. That’s the power of optimization ⚡ 💡 Insight: Understanding how to reduce problem size is the key to writing efficient algorithms. Even the simplest problems build the strongest foundation. Consistency is the real key 🔑 #DSA #Java #LeetCode #CodingJourney #BinarySearch #ProblemSolving #SoftwareEngineering #Day1
To view or add a comment, sign in
-
-
🚀 Day 13 of My DSA Journey Today I solved the “Power of Four” problem on LeetCode using Java. 💡 Problem Statement Given an integer n, determine whether it is a power of four. A number is a power of four if: n=4xn = 4^xn=4xfor some integer x. Example: Input: 16 → Output: true (because 42=164^2 = 1642=16) Input: 5 → Output: false ⚙️ Approach I Used I used a division-based approach. Steps: 1️⃣ First check if n is less than or equal to 0 → return false 2️⃣ While n is divisible by 4, keep dividing it by 4 3️⃣ After the loop, check if the result becomes 1 If it becomes 1, the number is a power of four. 📊 Complexity Analysis Time Complexity: O(log₄ n) because we keep dividing the number by 4. Space Complexity: O(1) ⚡ Result ✅ Accepted ⚡ Runtime: 0 ms (Beats 100%) 📚 Key Learning This problem helped reinforce number patterns and mathematical thinking, which are very useful in algorithmic problem solving. Consistency is slowly building stronger DSA fundamentals. On to Day 14 tomorrow. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #Algorithms #LearningInPublic #Consistency
To view or add a comment, sign in
-
Explore related topics
- LeetCode Array Problem Solving Techniques
- Leetcode Problem Solving Strategies
- Solving Sorted Array Coding Challenges
- Problem Solving Techniques for Developers
- How to Use Arrays in Software Development
- How to Improve Array Iteration Performance in Code
- Approaches to Array Problem Solving for Coding Interviews
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