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 find the maximum sum of a subarray in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Function maxSubArray(ByVal arr As Integer()) As Integer
        Dim maxSum As Integer = Integer.MinValue
        Dim currentSum As Integer = 0
        Dim size As Integer = arr.Length

        For i As Integer = 0 To size - 1
            currentSum = Math.Max(arr(i), currentSum + arr(i))
            maxSum = Math.Max(currentSum, maxSum)
        Next

        Return maxSum
    End Function

    Public Shared Sub Main(ByVal args As String())
		' 4 + -1 + -1 + 2 + 3 = 7
        Dim arr As Integer() = New Integer() {1, -2, 2, -3, 4, -1, -1, 2, 3, -5, 4}
	
        Console.WriteLine(maxSubArray(arr))
    End Sub
End Class

 

 
' run:
'
' 7
'

 

 



answered Feb 25, 2024 by avibootz

Related questions

1 answer 84 views
2 answers 187 views
1 answer 132 views
1 answer 43 views
...