ADVANCED DATA STRUCTURES PROGRAM • LEVEL 13 — ADVANCED SEARCHING
Search an Unbounded-Style Array Exponentially
Learn how to search an unbounded-style array exponentially using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
Index = 5
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int values[] = {2, 3, 4, 10, 40, 55, 70, 90}, target = 55, bound = 1;
while (bound < 8 && values[bound] < target) bound *= 2;
int low = bound / 2, high = bound < 7 ? bound : 7;
while (low <= high) { int middle = low + (high - low) / 2; if (values[middle] == target) { printf("Index = %d\n", middle); break; } if (values[middle] < target) low = middle + 1; else high = middle - 1; }
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Index = 5
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Double the bound until it crosses the target, then binary-search that bounded interval.
- Display the computed result.
Double the bound until it crosses the target, then binary-search that bounded interval.
EFFICIENCY
Time and space complexity
Time complexity
O(log position)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
