public class Solution {
    public static int firstOccurrence(int[] nums, int k) {
        int first = 0;
        int last = nums.length - 1;
        while (first < last - 1) {
            int med = first + (last - first) / 2;
            if (nums[med] < k) {
                first = med;
            } else {
                last = med;
            }
        }
        if (nums[first] == k) return first;
        else if (nums[last] == k) return last;
        else return -1;
    }

    public static void main(String[] args) {
        int[] nums = { 1, 1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 8, 10, 10, 11, 12, 16 };
        System.out.print("Array: [");
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i]);
            if (i != nums.length - 1) System.out.print(", ");
        }
        System.out.println("]");
        for (int k = 0; k < 20; k++) {
            int first = firstOccurrence(nums, k);
            System.out.println(k + " first found at index " + first);
        }
    }
}

@@ -162,20 +162,16 @@
 t %3C last
- - 1
 ) %7B%0A    
@@ -268,32 +268,107 @@
      first = med
+ + 1;%0A            %7D else if (nums%5Bmed%5D %3E k)%7B%0A                last = med - 1
 ;%0A            %7D 
@@ -474,55 +474,8 @@
 st;%0A
-        else if (nums%5Blast%5D == k) return last;%0A
     

Improve java solution