> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/hard/largest-range.md).

# Largest Range

![](/files/AnvydqfvUIAOSwnnQcrU)

* nlog, 1

```jsx
function largestRange(arr) {
  arr.sort((a, b) => a - b);
  console.log({ arr });
  
  let max = [arr[0], arr[0]];
  let range = [arr[0], arr[0]];
  
  for (let i = 1; i < arr.length; i++) {
    // same or diff is 1
    if (arr[i] - arr[i - 1]  <= 1) {
      // extend
      range[1] = arr[i];    
      continue;
    }
    console.log({
      range
    });

    // update
    const curr = Math.abs(max[1] - max[0]);
    const next = Math.abs(range[1] - range[0]);
    if (curr < next) {
      max = range;
    }

    // default
    range = [arr[i], arr[i]];
  }

      // update
  const curr = Math.abs(max[1] - max[0]);
  const next = Math.abs(range[1] - range[0]);
  if (curr < next) {
    max = range;
  }

  return max;
}

// Do not edit the line below.
exports.largestRange = largestRange;
```

* n, n

```jsx
function largestRange(arr) {
  let longestRange = [arr[0], arr[0]];
  
  const nums = [];
  for (const num of arr) {
    nums[num] = true;
  }

  // O(N) cuz when visted, we do not check anymore
  for (const num of arr) {
    // check if visited before
    if(!nums[num]) continue;
    nums[num] = false; // visited

    const currentRnage = [num, num];

    // find left
    let left = num - 1;
    while (nums[left]) {
      nums[left] = false; // visited
      currentRnage[0] = left;
      left--;
    }

    // find right
    let right = num + 1;
    while (nums[right]) {
      nums[right] = false; // visited
      currentRnage[1] = right;
      right++;
    }

    // check diff btw
    if (getAbsDiff(longestRange) >= getAbsDiff(currentRnage)) continue;

    // currentRage > longestRange
    longestRange = currentRnage;
  }

  return longestRange;
}

function getAbsDiff(arr) {
  return Math.abs(arr[1] - arr[0]);
}

// Do not edit the line below.
exports.largestRange = largestRange;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://algorithm.prettylog.com/algorithm-problems/algoexpert/hard/largest-range.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
