ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Binary Search for First Occurrence

R

Goal -- WPM

Ready
Exercise Algorithm Area
1find_first_occurrence <- function(sorted_vec, target) {
2low <- 1
3high <- length(sorted_vec)
4result_index <- -1
5
6# Handle empty vector edge case
7if (length(sorted_vec) == 0) {
8return(-1)
9}
10
11while (low <= high) {
12mid <- floor((low + high) / 2)
13
14if (sorted_vec[mid] == target) {
15# Found a potential match, store index and try to find an earlier one
16result_index <- mid
17high <- mid - 1 # Search in the left half for an earlier occurrence
18} else if (sorted_vec[mid] < target) {
19# Target is in the right half
20low <- mid + 1
21} else { # sorted_vec[mid] > target
22# Target is in the left half
23high <- mid - 1
24}
25}
26
27# Adjust index to be 0-based if found
28if (result_index != -1) {
29return(result_index - 1)
30} else {
31return(-1)
32}
33}
34
35# Example usage:
36# sorted_data <- c(2, 5, 5, 5, 6, 6, 8, 9, 9, 9)
37# target_val <- 5
38# index <- find_first_occurrence(sorted_data, target_val)
39# print(index) # Expected: 1 (index of the first 5)
40
41# Edge case: target not present
42# target_not_present <- 7
43# index_not_present <- find_first_occurrence(sorted_data, target_not_present)
44# print(index_not_present) # Expected: -1
45
46# Edge case: target at the beginning
47# target_first <- 2
48# index_first <- find_first_occurrence(sorted_data, target_first)
49# print(index_first) # Expected: 0
50
51# Edge case: target at the end
52# target_last <- 9
53# index_last <- find_first_occurrence(sorted_data, target_last)
54# print(index_last) # Expected: 7
55
56# Edge case: empty vector
57# empty_data <- numeric(0)
58# index_empty <- find_first_occurrence(empty_data, 5)
59# print(index_empty) # Expected: -1
Algorithm description viewbox

Binary Search for First Occurrence

Algorithm description:

This R function implements a modified binary search to find the index of the first occurrence of a target value in a sorted numeric vector. Standard binary search finds any occurrence, but this version is optimized to locate the leftmost instance. This is crucial in scenarios like searching for the start of a data range or the first instance of an event in time-series data.

Algorithm explanation:

The `find_first_occurrence` function uses a binary search strategy adapted to find the first occurrence of a `target` in a `sorted_vec`. It initializes `low` to the first index (1 in R's 1-based indexing) and `high` to the last index. The `result_index` is initialized to -1, indicating the target has not been found. The `while` loop continues as long as `low` is less than or equal to `high`. Inside the loop, `mid` is calculated. If `sorted_vec[mid]` equals `target`, we've found a potential first occurrence. We store this `mid` in `result_index` and then crucially set `high` to `mid - 1` to continue searching in the left half for an even earlier occurrence. If `sorted_vec[mid]` is less than `target`, the target must be in the right half, so `low` is updated. If `sorted_vec[mid]` is greater than `target`, the target must be in the left half, so `high` is updated. After the loop, if `result_index` is not -1, it means a target was found, and we return `result_index - 1` to convert it to a 0-based index. Otherwise, -1 is returned. The time complexity is O(log n) due to the binary search nature. The space complexity is O(1) as it uses a constant amount of extra space.

Pseudocode:

function find_first_occurrence(sorted_vec, target):
  low = 1
  high = length(sorted_vec)
  result_index = -1

  if length(sorted_vec) is 0:
    return -1

  while low <= high:
    mid = floor((low + high) / 2)

    if sorted_vec[mid] == target:
      result_index = mid
      high = mid - 1
    else if sorted_vec[mid] < target:
      low = mid + 1
    else:
      high = mid - 1

  if result_index != -1:
    return result_index - 1
  else:
    return -1