Binary search for 9 in sorted 1..18.
Toolliyo Coach
Progressive help: Nudge → Guide → Approach. Full solution stays behind the Solution tab.
Editor is open — no login wall to practice.
| Test | Status | Details |
|---|
Ready — edit the code above and click Run or Submit.
using System;
class Program
{
static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
int target = 9, lo = 0, hi = a.Length - 1, ans = -1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] == target) { ans = mid; break; }
if (a[mid] < target) lo = mid + 1; else hi = mid - 1;
}
Console.WriteLine(ans);
}
}
Prefer Coach (Nudge → Guide → Approach) before revealing. No forced signup.
O(log n) search on sorted array.
Sign in to save and review your submission history. You can still Run code on the Editor tab as a guest.
Sign in