/*
    An array of boolean values is divided into two sections; the left section consists of all false and the 
    right section consists of all true. Find the First True in a Sorted Boolean Array of the 
    right section, i.e. the index of the first true element. If there is no true element, return -1.
    
    Input: arr = [false, false, true, true, true]
    Output: 2

    Explanation: first true's index is 2.
*/

package main

import (
    "fmt"
)

func findBoundary(arr []bool) int {
    low := 0               // Initialize the low pointer to the beginning of the slice.
    high := len(arr) - 1   // Initialize the high pointer to the end of the slice.
    bv := -1               // Initialize bv (boundary value) to -1.

    for low <= high {
        mid := low + (high - low) / 2  // Calculate the middle index.

        if !arr[mid] {
            // If the element at the middle index is 'false',
            // it means that the last 'true' value should be on the right side.
            low = mid + 1               // Move the low pointer to the right of mid.
        } else {
            // If the element at the middle index is 'true',
            // update bv to the current middle index and continue searching on the left side.
            bv = mid                    // Update bv to the current middle index.
            high = mid - 1              // Move the high pointer to the left of mid.
        }
    }

    // The loop ends when low > high, indicating that the search is complete.
    // bv contains the index of the last 'true' value encountered.
    return bv
}

func main() {
    arr := []bool{false, false, false, true, true, true, true}
    boundary := findBoundary(arr)
    fmt.Println("Boundary Index:", boundary)
}
