Day 7: Solved Search Insert Position with Java and Binary Search

📅 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

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories