classSolution { public: boolcheck(longlong x, longlong y, longlong z, longlong n){ longlong num1 = y, num2 = n - y - 1; longlong sum1 = 0, sum2 = 0; if (num1 >= x - 1) { sum1 = (x - 1) * x / 2 + num1 - (x - 1); } else { sum1 = num1 * (x - num1 + x - 1) / 2; } if (num2 >= x - 1) { sum2 = (x - 1) * x / 2 + num2 - (x - 1); } else { sum2 = num2 * (x - num2 + x - 1) / 2; } return (sum1 + sum2 + x <= z); }
intmaxValue(int n, int index, int maxSum){ int l = 1, r = maxSum; while (l <= r) { int mid = l + r >> 1; if (check(mid, index, maxSum, n)) l = mid + 1; else r = mid - 1; } return r; } };