Easy
Binary Search
Topic: Searching •
Acceptance: 73%
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, return its index. Otherwise, return -1.
You must write an algorithm with O(log n) runtime complexity.
Input Format:
First line: n. Second line: n sorted integers. Third line: target.
Output Format:
Print index of target or -1.
Sample Example:
Sample Input:
6 -1 0 3 5 9 12 9Sample Output:
4
Sandboxed Engine
Ready to test. Click Test All Cases or Submit Solution to evaluate your code.
// Code Playground Cloud Execution Engine v2.4
// Write your code and click 'Run Code' to execute.
$ ready
Standard Input (stdin):
Your code can read standard input via
stdin.