Exception thows

package com.self;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

    int nos = sc.nextInt();
    int noc = sc.nextInt();

    int[] pos = new int[nos];

    for (int i = 0; i < pos.length; i++) {

        pos[i] = sc.nextInt();

    }

    Arrays.sort(pos);

    int low = 0;
    int high = pos[pos.length - 1] - pos[0];
    int postion = 0;

    while (low <= high) {
        int mid = (low + high) / 2;
        
        if (isItPossible(pos, noc, mid)) {
            postion = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }

    }
    System.out.println(postion);
}
public static boolean isItPossible(int[] pos, int noc, int mid) {
    int cowPlaced = 1;
    int lastCowPos = pos[0];

    for (int i = 1; i <= pos.length; i++) {

        if (pos[i] - lastCowPos >= mid) {
            cowPlaced++;
            lastCowPos = pos[i];
        }
        if (cowPlaced == noc) {
            return true;
        }
    }
    return false;
}

}