How to find the minimum distance between two numbers in an array with VB.NET

1 Answer

0 votes
Option Strict On

Imports System

''' <summary>
''' Provides utility methods for finding distances between values in an array.
''' </summary>
Public NotInheritable Class ArrayDistance

    ' Prevent instantiation of this utility class.
    Private Sub New()
    End Sub

    ''' <summary>
    ''' Finds the minimum distance between two values in an array.
    ''' </summary>
    ''' <param name="numbers">The array to search.</param>
    ''' <param name="target1">The first target value.</param>
    ''' <param name="target2">The second target value.</param>
    ''' <returns>
    ''' The minimum distance between the two values,
    ''' or -1 if no matching pair exists.
    ''' </returns>
    ''' <exception cref="ArgumentNullException">
    ''' Thrown when numbers is Nothing.
    ''' </exception>
    Public Shared Function FindMinimumDistance(numbers As Integer(), target1 As Integer, target2 As Integer) As Integer

        If numbers Is Nothing Then
            Throw New ArgumentNullException(NameOf(numbers))
        End If

        ' Special case: both target values are the same.
        If target1 = target2 Then
            Return FindMinimumDistanceBetweenDuplicates(numbers, target1)
        End If

        Dim lastSeenIndex As Integer = -1
        Dim minimumDistance As Integer = Integer.MaxValue

        For currentIndex As Integer = 0 To numbers.Length - 1

            Dim currentValue As Integer = numbers(currentIndex)

            ' Ignore values that are not one of the targets.
            If currentValue <> target1 AndAlso currentValue <> target2 Then
                Continue For
            End If

            '
            ' If we previously found one target and the current
            ' value is the other target, update the minimum distance.
            '
            If lastSeenIndex >= 0 AndAlso
               currentValue <> numbers(lastSeenIndex) Then

                Dim distance As Integer = currentIndex - lastSeenIndex

                minimumDistance = Math.Min(minimumDistance, distance)
            End If

            ' Store the most recent index.
            lastSeenIndex = currentIndex

        Next

        Return If(
            minimumDistance = Integer.MaxValue,
            -1,
            minimumDistance)
    End Function

    ''' <summary>
    ''' Finds the minimum distance between consecutive occurrences
    ''' of the same value.
    ''' </summary>
    Private Shared Function FindMinimumDistanceBetweenDuplicates(numbers As Integer(), target As Integer) As Integer

        Dim previousIndex As Integer = -1
        Dim minimumDistance As Integer = Integer.MaxValue

        For i As Integer = 0 To numbers.Length - 1

            If numbers(i) <> target Then
                Continue For
            End If

            If previousIndex >= 0 Then
                Dim distance As Integer = i - previousIndex
                minimumDistance =  Math.Min(minimumDistance, distance)
            End If

            previousIndex = i

        Next

        Return If(
            minimumDistance = Integer.MaxValue,
            -1,
            minimumDistance)
    End Function

End Class

Module Program

    Sub Main()

        Dim numbers As Integer() = {2, 3, 7, 3, 3, 4, 8, 8, 8, 11, 9, 1, 3}

        Dim result As Integer = ArrayDistance.FindMinimumDistance(numbers, 3, 8)

        If result >= 0 Then
            Console.WriteLine($"Minimum distance between 3 and 8: {result}")
        Else
            Console.WriteLine("No valid pair found.")
        End If

    End Sub

End Module


' run:
'
' Minimum distance between 3 and 8: 2
'

 



answered Aug 24, 2022 by avibootz
edited Oct 21 by avibootz

Related questions

...