You are given a 2D integer array stockPrices where stockPrices[i] = [dayi, pricei] indicates the price of the stock on day dayi is pricei. A line chart is created from the array by plotting the points on an XY plane with the X-axis representing the day and the Y-axis representing the price and connecting adjacent points.

public int minimumLines(int[][] stockPrices) {

Arrays.sort(stockPrices, (a, b) -> Integer.compare(a[0], b[0]));

int ans = stockPrices.length - 1;

for (int i = 2; i < stockPrices.length; i++) {

int a = (stockPrices[i][1] - stockPrices[i-1][1]);

int b = (stockPrices[i][0] - stockPrices[i-1][0]);

int c = (stockPrices[i-1][1] - stockPrices[i-2][1]);

int d = (stockPrices[i-1][0] - stockPrices[i-2][0]);

int ab = gcd(a, b);

int cd = gcd(c, d);

a /= ab; b /= ab; c /= cd; d /= cd;

if (a == c && b == d) ans -= 1;

}

return ans;

}

int gcd(int n1, int n2) {

if (n2 == 0)return n1;

return gcd(n2, n1 % n2);

}