Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,984 questions

51,928 answers

573 users

How to find the nearest smaller element on left side of each element of an array in VB.NET

1 Answer

0 votes
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
'

 



answered Dec 19, 2021 by avibootz
edited Dec 19, 2021 by avibootz
...