Imports System
Module Module1
Public Function MaxProductPair(arr As Integer()) As (First As Integer, Second As Integer)
If arr Is Nothing OrElse arr.Length < 2 Then
Throw New ArgumentException("Array must contain at least two elements.")
End If
Dim max1 As Integer = Integer.MinValue
Dim max2 As Integer = Integer.MinValue
Dim min1 As Integer = Integer.MaxValue
Dim min2 As Integer = Integer.MaxValue
For Each x In arr
' Track two largest values
If x > max1 Then
max2 = max1
max1 = x
ElseIf x > max2 Then
max2 = x
End If
' Track two smallest values
If x < min1 Then
min2 = min1
min1 = x
ElseIf x < min2 Then
min2 = x
End If
Next
Dim prodMax As Long = CLng(max1) * max2
Dim prodMin As Long = CLng(min1) * min2
If prodMax >= prodMin Then
Return (max1, max2)
Else
Return (min1, min2)
End If
End Function
Sub Main()
Dim arr = { 3, 9, 1, 3, 8, 0, 4 }
Dim pair = MaxProductPair(arr)
Console.WriteLine($"Max product pair: {pair.First}, {pair.Second}")
End Sub
End Module
' run:
'
' Max product pair: 9, 8
'