Imports System
Public Class Program
Public Shared Function smaller_index(ByVal arr As Integer(), ByVal pos As Integer) As Integer
Dim i As Integer = pos -1, index As Integer = -1
While i >= 0 AndAlso index = -1
If arr(i) < arr(pos) Then
index = i
End If
i -= 1
End While
Return index
End Function
Public Shared Sub nearest_smaller(ByVal arr As Integer())
Dim index As Integer = 0, size As Integer = arr.Length
For i As Integer = 0 To size - 1
index = smaller_index(arr, i)
If index = -1 Then
Console.WriteLine(arr(i) & " : No Smaller")
Else
Console.WriteLine(arr(i) & " : " & arr(index))
End If
Next
End Sub
Public Shared Sub Main()
Dim arr As Integer() = {4, 6, 2, 8, 6, 1, 9, 12, 3, 20, 18, 30}
nearest_smaller(arr)
End Sub
End Class
' run:
'
' 4 : No Smaller
' 6 : 4
' 2 : No Smaller
' 8 : 2
' 6 : 2
' 1 : No Smaller
' 9 : 1
' 12 : 9
' 3 : 1
' 20 : 3
' 18 : 3
' 30 : 18
'