Find smallest interval between 2 entity in one List<int>
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Runtime.Serialization;
public class Solution {
public static int FindSmallestInterval(List<int> numbers) {
// Write your code here
if(numbers.Count < 2 || numbers.Count >100000){
throw new ArgumentException("invalid input");
}
var arrayNumbers = numbers.ToArray();
Array.Sort(arrayNumbers);
int toReturn = int.MaxValue;
for (int i = 1; i < arrayNumbers.Length; i++)
{
toReturn = Math.Min(toReturn, arrayNumbers[i] - arrayNumbers[i - 1]);
}
return toReturn;
}
Aucun commentaire:
Enregistrer un commentaire