1. Linear Scan (Brute Force)
Intuition
Since duplicates in a rotated sorted array degrade the worst-case binary search to O(n), a straightforward linear scan checks every element in O(n) time and O(1) space.
When to apply: Use as the baseline reference comparison for search correctness.
Linear Scan (O(N) Time)
2 of 2 questions left todayExamine the first slot
Examine the first slot. Value 2 is not the target.
public class Main { public static class Solution { public boolean search(int[] nums, int target) { for (int x : nums) { if (x == target) return true; } return false; } } public static void main(String[] args) { Solution sol = new Solution(); int[] nums = {2, 5, 6, 0, 0, 1, 2}; int target = 0; System.out.println("Search Result: " + sol.search(nums, target)); }}Time & Space Complexity
- Time complexity:
O(n)— A single linear pass inspects up to n elements. - Space complexity:
O(1)— O(1) auxiliary variables.
Why Brute Force Doesn't Scale:
For N = 105, quadratic O(N2) approaches require ~1010 operations, exceeding the standard 1-second CPU budget.
The optimal approach processes N elements in a single or logarithmic pass, completing in under 10ms with minimal memory footprint.