Imports System
Imports System.Collections.Generic
Public Class Program
Public Shared Function countPairsWhoseProductsExistInArray(ByVal arr As Integer()) As Integer
Dim total As Integer = 0, size As Integer = arr.Length
Dim Hash As HashSet(Of Integer) = New HashSet(Of Integer)()
For i As Integer = 0 To size - 1
Hash.Add(arr(i))
Next
For i As Integer = 0 To size - 1
For j As Integer = i + 1 To size - 1
Dim product As Integer = arr(i) * arr(j)
If Hash.Contains(product) Then
total += 1
End If
Next
Next
Return total
End Function
Public Shared Sub Main(ByVal args As String())
Dim arr As Integer() = New Integer() {2, 8, 5, 16, 6, 3, 7, 30}
' 2 * 8 = 16
' 2 * 3 = 6
' 5 * 6 = 30
Console.WriteLine("Total = " & countPairsWhoseProductsExistInArray(arr))
End Sub
End Class
' run:
'
' Total = 3
'