Repository navigation
#060 Max Min Difference
Given an array of integers, return the difference between the largest and smallest integers in the array.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/060 Max Min Difference.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//Given an array of integers, return the difference between the largest and smallest integers in the array.
function difference(nums) {
return Math.max(...nums) - Math.min(...nums);
}
import * as Test from '../testHelper.js';
Test.assertEquals(difference([-9, -8, 6, -9, 15, 6]), 24)
Test.assertEquals(difference([-5, 6, 18, 4, 16, -2]), 23)
Test.assertEquals(difference([-2, 20, -9, -9, -2, -7]), 29)
Test.assertEquals(difference([4, -2, 11, -9, 15, 2]), 24)
Test.assertEquals(difference([15, 10, 3, -6, 6, 19]), 25)
Test.assertEquals(difference([1, 7, 18, -1, -2, 9]), 20)
Test.assertEquals(difference([5, 1, -9, 7, -8, -10]), 17)
Test.assertEquals(difference([-4, 17, -4, 20, -7, 0]), 27)
Test.assertEquals(difference([-2, 11, 11, -3, -3, -3]), 14)
Test.assertEquals(difference([1, 15, 14, 20, 10, 6]), 19)
Test.assertEquals(difference([4, 17, 12, 2, 10, 2]), 15)
Test.assertEquals(difference([-3, 3, 20, 10, 0, 17]), 23)
Test.assertEquals(difference([-3, 6, 20, 9, 6, 7]), 23)
Test.assertEquals(difference([16, 15, 1, 18, -7, -3]), 25)
Test.assertEquals(difference([-7, 4, -4, -3, -8, -9]), 13)
Test.assertEquals(difference([15, 8, 17, 18, 10, 10]), 10)
Test.assertEquals(difference([-3, 20, 16, 8, 18, -10]), 30)
Test.assertEquals(difference([6, 18, 9, 1, 3, 1]), 17)
Test.assertEquals(difference([20, 18, -2, -10, -10, 17]), 30)
Test.assertEquals(difference([18, 20, -7, -4, -2, -8]), 28)Cross-language explanation preserved from the study materials
Reference file: #054 Max Min Difference.txt. These older notes use a separate numbering scheme and may describe C# code, external diagrams or benchmark timings. They are reference material, not measured JavaScript performance. The current implementation and assertions above are authoritative.
# [Max Min Difference](https://edabit.com/challenge/LBeNrzWBXkYp4kwYj)
Published by [Someone1](https://edabit.com/user/bi7cvQ3sziZHxhXJa)
arrays math numbers
Given an array of integers, return the difference between the largest and smallest integers in the array.
```
Diff([10, 15, 20, 2, 10, 6]) ➞ 18
// 20 - 2 = 18
```
```
Diff([-3, 4, -9, -1, -2, 15]) ➞ 24
// 15 - (-9) = 24
```
`Diff([4, 17, 12, 2, 10, 2]) ➞ 15`
### Time Complexity:
Both `arr.Max()` and `arr.Min()` functions iterate through the array to find the maximum and minimum values, each taking _O(n)_ time, where
𝑛 is the number of elements in arr. Thus, the overall complexity is _O(2n)_. Operations are independent, but method requires **two full passes** through the array. Manual approach can do it in a single pass, avoid the abstraction and overhead of LINQ and take the advantage of low-level optimizations that are harder to achieve with higher-level LINQ methods. This results in twice as fast implementation!
### Space Complexity:
This function only requires a constant amount of space for storing the result of `arr.Max()` and `arr.Min()`, so the space complexity is _O(1)_. The function does not create additional data structures that grow with the input size.

Manual approach:

//Given an array of integers, return the difference between the largest and smallest integers in the array.
using BenchmarkDotNet.Attributes;
using System;
using System.Collections;
using System.Linq;
namespace Challenges
{
public class Program54
{
public static int Diff(int[] arr)
{
int max = int.MinValue;
int min = int.MaxValue;
for (int i = 0; i < arr.Length; i++)
{
int num = arr[i];
if (num > max) max = num;
if (num < min) min = num;
}
return max - min;
}
}
public class BenchmarkProgram54
{
[Benchmark]
[Arguments(new int[] { -9, -8, 6, -9, 15, 6 })]
[Arguments(new int[] { -5, 6, 18, 4, 16, -2 })]
[Arguments(new int[] { -2, 20, -9, -9, -2, -7 })]
[Arguments(new int[] { 4, -2, 11, -9, 15, 2 })]
[Arguments(new int[] { 15, 10, 3, -6, 6, 19 })]
[Arguments(new int[] { 1, 7, 18, -1, -2, 9 })]
[Arguments(new int[] { 5, 1, -9, 7, -8, -10 })]
[Arguments(new int[] { 5, 1, -9, 7, -8, -10 })]
[Arguments(new int[] { 4, 17, 12, 2, 10, 2 })]
public int Diff(int[] arr) => Program54.Diff(arr);
}
}
//Given an array of integers, return the difference between the largest and smallest integers in the array.
using NUnit.Framework;
using System;
using Challenges.Edabit;
namespace Tests
{
[TestFixture]
public class Tests54
{
[Test]
[TestCase(new int[] { -9, -8, 6, -9, 15, 6 }, 24)]
[TestCase(new int[] { -5, 6, 18, 4, 16, -2 }, 23)]
[TestCase(new int[] { -2, 20, -9, -9, -2, -7 }, 29)]
[TestCase(new int[] { 4, -2, 11, -9, 15, 2 }, 24)]
[TestCase(new int[] { 15, 10, 3, -6, 6, 19 }, 25)]
[TestCase(new int[] { 1, 7, 18, -1, -2, 9 }, 20)]
[TestCase(new int[] { 5, 1, -9, 7, -8, -10 }, 17)]
[TestCase(new int[] { 5, 1, -9, 7, -8, -10 }, 17)]
[TestCase(new int[] { 4, 17, 12, 2, 10, 2 }, 15)]
public void Diff(int[] arr, int expectedResult)
{
int result = Program54.Diff(arr);
Assert.That(result, Is.EqualTo(expectedResult));
}
}
}