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

51,876 answers

573 users

How to find subarray with given sum in VB.NET

1 Answer

0 votes
Imports System

Public Class Test
    Shared Dim start_i As Integer = 0, end_i As Integer = 0
     
    Public Shared Sub find_sub_array(arr() As Integer, sum As Integer) 
       Dim current_sum As Integer = 0, len As Integer = arr.Length
        
       For i As Integer = 0 To len
            current_sum = arr(i)  
            For j As Integer = i + 1 To len  
                If current_sum = sum Then  
                    start_i = i
                    end_i = j - 1
                    Exit Sub
                End If
                If current_sum > sum Or j = len Then  
                    Exit For  
                End If
                current_sum += arr(j)  
            Next
        Next
    End Sub 
    
    Public Shared Sub Main()
        Dim arr() As Integer = {6, 1, 4, -1, 5, -1, 2}
        Dim sum As Integer = 9
         
        find_sub_array(arr, sum)
         
        Console.WriteLine("from index {0} to {1}", start_i, end_i) 
    End Sub
End Class



' run:
'
' from index 1 to 4

 



answered Jul 20, 2019 by avibootz
edited Jul 20, 2019 by avibootz

Related questions

...