def binary_search(nums, target):
    """
    Find first position of target in given array by binary search

    :param nums: given array
    :type nums : list[int]
    :param target: target number
    :type target: int
    :return: first position of target
    :rtype: int
    """
    left, right = 0, len(nums) - 1
    while left <= right:
        mid = (left + right) // 2
        # note that we move right pointer when nums[mid] == target
        # to find the first occurrence of target
        if nums[mid] >= target:
            right = mid - 1
        else:
            left = mid + 1

    if nums[left] == target:
        return left
    else:
        return -1