How to check if product of every adjacent pairs exists in an array with VB.NET

2 Answers

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub addToSet(ByVal arr As Integer(), ByVal hset As HashSet(Of Integer))
        For i As Integer = 0 To arr.Length - 1
            hset.Add(arr(i))
        Next
    End Sub

    Private Shared Function check_product_of_every_pair(ByVal array As Integer()) As Boolean
        Dim hset As HashSet(Of Integer) = New HashSet(Of Integer)()
        addToSet(array, hset)

        For i As Integer = 0 To array.Length - 1 Step 2
            Dim product As Integer = array(i) * array(i + 1)
            Console.WriteLine(array(i) & " * " & array(i + 1) & " = " & product)
            If Not hset.Contains(product) Then
                Return False
            End If
        Next

        Return True
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim array As Integer() = New Integer() {2, 3, 6, 5, 30, 0}

        Console.Write((If(check_product_of_every_pair(array), "Yes", "No")))
    End Sub
End Class





' run:
'
' 2 * 3 = 6
' 6 * 5 = 30
' 30 * 0 = 0
' Yes
'

 



answered Jul 23, 2023 by avibootz
edited Jul 23, 2023 by avibootz
0 votes
Imports System
Imports System.Linq

Public Class Program
    Private Shared Function check_product_of_every_pair(ByVal array As Integer()) As Boolean
        For i As Integer = 0 To array.Length - 1 Step 2
            Dim product As Integer = array(i) * array(i + 1)
            Console.WriteLine(array(i) & " * " & array(i + 1) & " = " & product)

            If Not array.Contains(product) Then
                Return False
            End If
        Next

        Return True
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim array As Integer() = New Integer() {2, 3, 6, 5, 30, 0}
	
        Console.Write((If(check_product_of_every_pair(array), "Yes", "No")))
    End Sub
End Class





' run:
'
' 2 * 3 = 6
' 6 * 5 = 30
' 30 * 0 = 0
' Yes
'

 



answered Jul 23, 2023 by avibootz
...