For this Question: Given a sorted array, find the difference between the last and first occurrence of the given number.
<b>Note:<b> 
There will be at least one repeated element.The first line contains integer n, the size of the array.
The second line contains array elements.
The third line contains the number itself.

<b>Constraints</b>
2 &le; n &le; 10<sup>8</sup>
0 &le; array_elements &le; 10<sup>3</sup>

<b>Note:</b>
Array size is large, try to come up with most optimal solution.Print a single integer the difference between the last and first occurrence of the given number.<b>Sample Input: </b>
6
1 1 1 2 2 2
2

<b>Sample Output </b>
2

<b>Explanation :</b>
The first occurrence of 2 is at index 3 and the last is at 5. So, the difference is (5-3) equals 2., I have written this Solution Code: import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // take input from user
       
        int n = sc.nextInt();
        int[] arr = new int[n];
        
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
    
        int x = sc.nextInt();
        
        // find first occurrence of x
        int first = firstOccurrence(arr, x);
        if (first == -1) {
            System.out.println(-1);
            return;
        }
        
        // find last occurrence of x
        int last = lastOccurrence(arr, x);
        
        // calculate difference between first and last occurrence
        int diff = last - first;
        System.out.println(diff);
    }
    
    // binary search to find first occurrence of x in arr
    public static int firstOccurrence(int[] arr, int x) {
        int low = 0, high = arr.length - 1, result = -1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (arr[mid] == x) {
                result = mid;
                high = mid - 1;
            } else if (arr[mid] < x) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return result;
    }
    
    // binary search to find last occurrence of x in arr
    public static int lastOccurrence(int[] arr, int x) {
        int low = 0, high = arr.length - 1, result = -1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (arr[mid] == x) {
                result = mid;
                low = mid + 1;
            } else if (arr[mid] < x) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return result;
    }
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not?