Skip to content
Squarebrackets Private Limited
Squarebrackets
Courses
Browse
Explore
Services
Learning
Prep
Code&Run
Whiteboard
LMS
Problems

Binary Search

Easy1 pt
ArrayBinary Search
PrerequisitesBinary Search, Array
Return the index of target in a sorted array, or -1 if absent. Input: line 1 = sorted integers; line 2 = target. Output: 0-based index or -1.
Example 1
Input
-1 0 3 5 9 12
9
Output
4
Explanation
For the given input, the program prints exactly this output.
Example 2
Input
-1 0 3 5 9 12
2
Output
-1
Explanation
A second case — verify your logic handles it too.
Hints
Video
8 lines
Input (stdin)
Expected output · optional
Run checks your code against every example, one by one — all must pass. Type your own input above to run that instead (Run compares it to the expected output if you set one). The full suite is checked on Submit — a failing case reveals its input & expected output.