How to implement the two sum algorithm to find two values in array that add up to target with VB.NET

3 Answers

0 votes
Imports System

Public Class Program
    Private Shared Function twoSum(ByVal array As Integer(), ByVal target As Integer) As Integer()
        For i As Integer = 0 To array.Length - 1
            For j As Integer = i + 1 To array.Length - 1
                If array(i) + array(j) = target Then
                    Return New Integer() {i, j}
                End If
            Next
        Next

        Return New Integer() {}
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim array As Integer() = New Integer() {1, 5, 7, 6, 4, 3, 2}
        Dim target As Integer = 9
        Dim indexes As Integer() = twoSum(array, target)

        If indexes.Length = 2 Then
            Console.WriteLine(indexes(0) & " " & indexes(1))
        Else
            Console.WriteLine("Not found")
        End If
    End Sub
End Class



' run:
'
' 1 4
'

 



answered Jul 17, 2023 by avibootz
0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Private Shared Function twoSum(ByVal array As Integer(), ByVal target As Integer) As Integer()
        Dim hmap As IDictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer)()

        For i As Integer = 0 To array.Length - 1
            Dim complement As Integer = target - array(I)

            If hmap.ContainsKey(complement) Then
                Return New Integer() {hmap(complement), i}
            Else
                hmap(array(i)) = i
            End If
        Next

        Return New Integer() {}
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim array As Integer() = New Integer() {1, 5, 7, 6, 4, 3, 2}
        Dim target As Integer = 9
        Dim indexes As Integer() = twoSum(array, target)

        If indexes.Length = 2 Then
            Console.WriteLine(indexes(0) & " " & indexes(1))
        Else
            Console.WriteLine("Not found")
        End If
    End Sub
End Class




' run:
'
' 1 4
'

 



answered Jul 17, 2023 by avibootz
0 votes
Imports System

Public Class Program
    Private Shared Function twoSum(ByVal array As Integer(), ByVal target As Integer) As Integer()
        Array.Sort(array)
        Dim left As Integer = 0, right As Integer = array.Length - 1

        While left < right
            If array(left) + array(right) = target Then
                Return New Integer() {array(right), array(left)}
            ElseIf array(right) + array(left) < target Then
                left += 1
            Else
                right -= 1
            End If
        End While

        Return New Integer() {}
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim array As Integer() = New Integer() {1, 5, 7, 6, 4, 38, 23}
        Dim target As Integer = 9
        Dim values As Integer() = twoSum(array, target)

        If values.Length = 2 Then
            Console.WriteLine(values(0) & " " & values(1))
        Else
            Console.WriteLine("Not found")
        End If
    End Sub
End Class





' run:
'
' 5 4
'

 



answered Jul 17, 2023 by avibootz
...