1. Sequential Reader Query
Intuition
Loop i from 0 upwards. Check reader.get(i). If equal to target, return i; if greater than target or sentinel, return -1.
When to apply: Baseline benchmark.
Algorithm
- Step 1: Set i = 0.
- Step 2: Read val = reader.get(i).
- Step 3: If val == target, return i.
- Step 4: If val > target, return -1.
- Step 5: i++, repeat.
#include <iostream>#include <vector>#include <string>#include <algorithm>using namespace std; int search(const ArrayReader& reader, int target) { int i = 0; while (true) { int val = reader.get(i); if (val == target) return i; if (val > target) return -1; i++; }} int main() { // Sample driver execution cout << "Execution completed successfully." << endl; return 0;} Time & Space Complexity
- Time complexity:
O(T)— Makes T sequential API queries. - Space complexity:
O(1)— O(1) memory.