{"size": 529, "ext": "js", "max_stars_repo_path": "javascript/_1658.js", "max_stars_repo_name": "shisheng-1/Leetcode", "max_stars_repo_stars_event_min_datetime": "2016-12-07T18:36:12.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T12:31:33.000Z", "max_issues_repo_path": "javascript/_1658.js", "max_issues_repo_name": "Reeshma-M/Leetcode", "max_issues_repo_issues_event_min_datetime": "2017-08-08T19:52:13.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-04T17:10:25.000Z", "max_forks_repo_path": "javascript/_1658.js", "max_forks_repo_name": "Reeshma-M/Leetcode", "max_forks_repo_forks_event_min_datetime": "2016-12-07T19:09:40.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-27T06:31:14.000Z"}
{"max_stars_count": 3508, "max_issues_count": 62, "max_forks_count": 1389, "avg_line_length": 20.3461538462, "max_line_length": 63, "alphanum_fraction": 0.5274102079}
// Author: Phuong Lam

/**
 * @param {number[]} nums
 * @param {number} x
 * @return {number}
 */
var minOperations = function (nums, x) {
  const total = nums.reduce((a, b) => a + b)
  if (total === x) return nums.length

  var sum = 0
  var head = 0
  var max = -1
  for (var tail = 0; tail < nums.length; tail++) {
    sum += nums[tail]
    while (sum > total - x) {
      sum -= nums[head]
      head++
    }
    if (sum === total - x) max = Math.max(max, tail - head + 1)
  }

  return max === -1 ? -1 : nums.length - max
}
