Description
Binary Search Challenge
Problem Description
Implement a binary search algorithm to find a target value in a sorted array of integers.
Task
Write a function binarySearch(arr, target)
that takes two parameters:
arr
: A sorted array of integers in ascending ordertarget
: The integer value to search for in the array
The function should return:
- The index of the target value if it exists in the array
- -1 if the target value is not found in the array
Input
- The first line contains an integer
n
(1 ≤ n ≤ 10^5), the number of elements in the array. - The second line contains
n
space-separated integersa_i
(-10^9 ≤ a_i ≤ 10^9), representing the sorted array. - The third line contains an integer
q
(1 ≤ q ≤ 10^5), the number of queries. - Each of the next
q
lines contains a single integert_i
(-10^9 ≤ t_i ≤ 10^9), the target value to search for.
Output
For each query, output a single integer on a new line:
- The index (0-based) of the target value in the array if found
- -1 if the target value is not in the array
Constraints
- Time limit per test: 3 second
- Memory limit per test: 256 megabytes
Sample Input
10 -5 -2 0 3 6 9 11 14 18 21 11
Sample Output
6
Start & End Times
Start Time | 2024-07-20 19:58 CEST |
End time | 2024-07-22 00:58 CEST |
Problems
No problems available.
Scoring
Partial Credit — Ranked
Explanation:
Standard scoreboard type for problems with partial credit. Participants are ranked by the sum of the score of each problem. There is no tiebreaker.
Standings
- Standings are shown without limitation.
Languages
Ada Algol 68 APL Bash C C# C++ COBOL Common Lisp Crystal D Dart Elixir Erlang F# Forth Fortran Go Haskell Java JavaScript (Node.js) JavaScript (SpiderMonkey) Julia Kotlin Lua Modula-2 Nim Objective-C OCaml Octave Odin Pascal Perl PHP Prolog Python 3 Racket Ruby Rust Simula 67 Smalltalk SNOBOL Swift TypeScript Visual Basic Zig