How to search in a rotated sorted array using binary search

💡 #GFG160 Day 30 — Search in Rotated Sorted Array Today’s challenge focused on finding a target element in a sorted and rotated array — one of the most elegant uses of binary search. 🧩 Problem: Given a rotated sorted array, find the index of the target element. If it’s not present, return -1. Example: arr = [4,5,6,7,0,1,2], target = 0 → Output: 4 ⚙️ Approach / Intuition: Even after rotation, one half of the array is always sorted. So in each step: Find the mid element. Check which half (left or right) is sorted. Decide where to move next based on the target’s range. This approach eliminates half of the search space every iteration — pure binary search efficiency. 🧩 Dry Run (Example): arr = [4,5,6,7,0,1,2], target = 0 mid = 7 → left half sorted target not in left → move right mid = 1 → right half sorted target in left → move left mid = 0 → found ✅ ⏱ Time Complexity: O(log N) 💾 Space Complexity: O(1) 💻 Language: Java 📚 Key takeaway: Binary Search is not just for plain sorted arrays — the trick lies in identifying which half is sorted each time! #GeeksforGeeks #Java #BinarySearch #CodingChallenge #DSA #ProblemSolving #LearningEveryday

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories