int binarySearch(int a[], int len, int key)
{
    int left = 1;
    int right = len;
    int mid;

    while (left <= right) {
        mid = (left + right) / 2;
        if (key < a[mid]) {
            right = mid - 1;
        } else if (a[mid] < key) {
            left = mid + 1;
        } else {
            return mid;
        }
    }
    return -1;
}