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

51,826 answers

573 users

How to find the index that split an array into two equal sum subarrays in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Private Shared Function getSplitIndex(ByVal arr As Integer()) As Integer
        Dim size As Integer = arr.Length
        Dim leftSum As Integer = 0

        For i As Integer = 0 To size - 1
            leftSum += arr(i)

			Dim rightSum As Integer = 0
            For j As Integer = i + 1 To size - 1
                rightSum += arr(j)
            Next

            If leftSum = rightSum Then Return i + 1
        Next

        Return -1
    End Function

    Private Shared Sub printSplitParts(ByVal arr As Integer())
        Dim size As Integer = arr.Length
        Dim splitIndex As Integer = getSplitIndex(arr)

        If splitIndex = -1 OrElse splitIndex = size Then
            Console.Write("No equal parts")
            Return
        End If

        For i As Integer = 0 To size - 1
            If splitIndex = i Then Console.WriteLine()
            Console.Write(arr(i) & " ")
        Next
    End Sub

    Public Shared Sub Main()
        Dim arr1 As Integer() = {1, 2, 3, 4, 5, 5}
        printSplitParts(arr1)
	
        Console.WriteLine()
	
        Dim arr2 As Integer() = {1, 2, 3, 4, 5, 5, 1}
        printSplitParts(arr2)
    End Sub
End Class

 



answered Mar 11, 2021 by avibootz
...