Solved Binary Search problem in 100DaysOfCode

🌟 Day 24 of #100DaysOfCode. Today I come with one challenging problem which is related to the topic Binary Search. 🌟 ✔️ What I solved: *️⃣ Problem of the Day: Binary Search ✴️ Approach & Key Insights: 🔹 Binary Search divides the array into halves to efficiently search for a target in O(log n) time. 🔹 The sorted property is crucial — binary search only works on sorted arrays. ◾ Steps: 🔹 Initialize two pointers, left at the start and right at the end of the array. While left <= right: 🔹 Compute the middle index mid = left + (right - left) // 2. 🔹 If nums[mid] is the target, return mid. 🔹If nums[mid] < target, search right half (left = mid + 1). 🔹If nums [mid] > target, search left half (right = mid - 1). 🔹If you exit the loop, target is not present. Return -1. ✴️ Complexity: 🔹 Time Complexity: O(log⁡n) 🔹 Space Complexity: O(1) ✴️ Key Takeaways: 🔹 Use binary search for efficient lookups in sorted arrays. Always check for overflow when computing mid: mid = left + (right - left) / 2. 🔹 The standard approach is iterative for minimal space, but recursive solutions work as well. 🚀For more information, Click here ⬇️ https://lnkd.in/gaQTpVtk 🪄 I am very thankful to my Mentor Mr.Rajesh Gupta Sir and KR Mangalam University for giving me the support throughout this week. #DSA #100DaysOfCode #KRMU #Competitive #Coding

  • text

To view or add a comment, sign in

Explore content categories