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

51,879 answers

573 users

How to count the total pairs whose products exist in an array with VB.NET

1 Answer

0 votes
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
'

 



answered Jun 16, 2024 by avibootz
...