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

1 Answer

0 votes
Imports System
 
Public Class Program
    Public Shared Function MinDistance(ByVal arr As Integer(), number1 As Integer, number2 As Integer) As Integer
        Dim size As Integer = arr.Length
         
        Dim min_distance As Integer = Integer.MaxValue
 
        For i As Integer = 0 To size - 1
            For j As Integer = i + 1 To size - 1
                If arr(i) = number1 AndAlso arr(j) = number2 Or 
					arr(i) = number2 AndAlso arr(j) = number1 Then
                    min_distance = Math.Min(min_distance, Math.Abs(i - j))
                End If
            Next
        Next
 
        Return min_distance
    End Function
 
    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {2, 3, 7, 3, 3, 4, 8, 8, 8, 11, 9, 1, 3}
 
        Console.WriteLine(MinDistance(arr, 3, 8))
    End Sub
End Class
 
 
 
 
' run:
'
' 2
'

 



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

Related questions

...