SearchCademy

Can someone help me pls with this task? Line number 16, i wrote the task near it.

func sparseSearch<T: Comparable>(for target: T, in arr: [T], isSparse: (T) → Bool) → Int? {
var first = 0
var last = arr.count
while first <= last {
var middle = (first + last) / 2
if !isSparse(arr[middle]) {
var left = middle - 1
var right = middle + 1
while true {
if left < first && right > last {
return nil
//??? 16 line task is:
/*Add an else if statement that checks if the value at index right is not sparse and that right is less than or equal to last. If so, assign middle to right and break out of the while loop.
*/
} else if !isSparse(arr[right]) && right <= last { //here
middle = right
break
}
right += 1
left -= 1
}
}
if arr[middle] == target {
return middle
}
if target < arr[middle] {
last = middle - 1
}
if target > arr[middle] {
first = middle + 1
}
}
return nil
}

let arr = [“Arthur”, “”, “”, “”, “”, “Devan”, “”, “”, “Elise”, “”, “”, “”, “Gary”, “”, “”, “Mimi”, “”, “”, “Parth”, “”, “”, “”, “Zachary”]
let target = “Devan”
if let targetIndex = sparseSearch(for: target, in: arr, isSparse: { $0 == “” }) {
print(“(target) found at index (targetIndex)”)
} else {
print(“(target) not found”)
}

example of the working code of this task

func sparseSearch<T: Comparable>(for target: T, in arr:[T], isSparse:(T) → Bool) → Int? {
var first = 0
var last = arr.count
while first <= last {
var middle = (first + last) / 2
if isSparse(arr[middle]) { // without “!” in isSparse
var left = middle - 1
var right = middle + 1
while true {
if left < first && right > last {
return nil
} else if !isSparse(arr[right]) && right <= last {
middle = right
break
} else if !isSparse(arr[left]) && left >= first {
middle = left
break
}
right += 1
left -= 1
}
}
if arr[middle] == target {
return middle
}
if target < arr[middle] {
last = middle - 1
} else {
first = middle + 1
}
}
return nil
}

let arr = [“Arthur”, “”, “”, “”, “”, “Devan”, “”, “”, “Elise”, “”, “”, “”, “Gary”, “”, “”, “Mimi”, “”, “”, “Parth”, “”, “”, “”, “Zachary”]
let target = “Arthur”
if let targetIndex = sparseSearch(for: target, in: arr, isSparse: { $0 == “” }) {
print(“(target) found at index (targetIndex)”)
} else {
print(“(target) not found”)
}