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 get maximum product of subarray in VB.NET

1 Answer

0 votes
Imports System

Public Class MaxProductOfSubarray_CSharp
    Public Shared Function getMaxProductOfSubarray(ByVal arr As Integer()) As Integer
        Dim len As Integer = arr.Length

        If len = 0 Then
            Return 0
        End If

        Dim max_current_index As Integer = arr(0), min_current_index As Integer = arr(0), max_product As Integer = arr(0)
        Console.Write("arr[i] = {0:D} max_current_index = {1:D}  min_current_index = {2:D} max_product = {3:D}" & Environment.NewLine, arr(0), max_current_index, min_current_index, max_product)

        For i As Integer = 1 To len - 1
            Dim temp As Integer = max_current_index
		
            max_current_index = Math.Max(arr(i), Math.Max(arr(i) * max_current_index, arr(i) * min_current_index))
            min_current_index = Math.Min(arr(i), Math.Min(arr(i) * temp, arr(i) * min_current_index))
		
            Console.Write("arr[i] = {0:D} max_current_index = {1:D}  min_current_index = {2:D}" & Environment.NewLine, arr(i), max_current_index, min_current_index)
		
            max_product = Math.Max(max_product, max_current_index)
		
            Console.Write("max_product = {0:D}" & Environment.NewLine, max_product)
        Next

        Return max_product
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {-4, 8, 2, 5, 90, 7, 0, -3, 6} ' 8 * 2 * 5 * 90 * 7 = 50400
	
        Console.Write("maximum product = " & getMaxProductOfSubarray(arr))
    End Sub
End Class




' run:
'
' arr[i] = -4 max_current_index = -4  min_current_index = -4 max_product = -4
' arr[i] = 8 max_current_index = 8  min_current_index = -32
' max_product = 8
' arr[i] = 2 max_current_index = 16  min_current_index = -64
' max_product = 16
' arr[i] = 5 max_current_index = 80  min_current_index = -320
' max_product = 80
' arr[i] = 90 max_current_index = 7200  min_current_index = -28800
' max_product = 7200
' arr[i] = 7 max_current_index = 50400  min_current_index = -201600
' max_product = 50400
' arr[i] = 0 max_current_index = 0  min_current_index = 0
' max_product = 50400
' arr[i] = -3 max_current_index = 0  min_current_index = -3
' max_product = 50400
' arr[i] = 6 max_current_index = 6  min_current_index = -18
' max_product = 50400
' maximum product = 50400
'

 



answered Jul 20, 2024 by avibootz
edited Jul 20, 2024 by avibootz

Related questions

1 answer 86 views
1 answer 79 views
1 answer 81 views
1 answer 89 views
1 answer 84 views
...