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,938 questions

51,875 answers

573 users

How to replace each element in array with the product of every other elements in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Sub product_of_every_other_elements(ByVal arr As Integer())
        Dim size As Integer = arr.Length

        If size = 0 Then
            Return
        End If

        Dim left As Integer() = New Integer(size - 1) {}
        Dim right As Integer() = New Integer(size - 1) {}
        left(0) = 1

        For i As Integer = 1 To size - 1
            left(i) = arr(i - 1) * left(i - 1)
        Next

        right(size - 1) = 1

		For j As Integer = size - 2 To 0 step -1
            right(j) = arr(j + 1) * right(j + 1)
        Next

        For i As Integer = 0 To size - 1
            arr(i) = left(i) * right(i)
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim array As Integer() = New Integer() {1, 2, 3, 4, 5}

        product_of_every_other_elements(array)

        For i As Integer = 0 To array.Length - 1
            Console.Write(array(i) & " ")
        Next
    End Sub
End Class




' run:
'
' 120 60 40 30 24
'

 



answered Sep 22, 2023 by avibootz
...