How to check if an array contains a pair of numbers whose product is odd in VB.NET

1 Answer

0 votes
' A product of a pair of integers will always be odd if at least 2 of them are odd

Imports System

Friend Class Program
    Public Shared Function arrayContainsAPairOfOddProduct(ByVal arr As Integer()) As Boolean
        Dim oddCount As Integer = 0

        For i As Integer = 0 To arr.Length - 1

            If arr(i) Mod 2 <> 0 Then
                oddCount += 1

                If oddCount = 2 Then
                    Exit For
                End If
            End If
        Next

        Return oddCount > 1
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {2, 2, 4, 4, 6, 6, 7, 10, 10, 9} ' 7 * 9 = 63 -> Odd
        
        Console.WriteLine(arrayContainsAPairOfOddProduct(arr))
    End Sub
End Class

 
   
   
' run:
'
' True
'

 



answered Jun 14, 2024 by avibootz
edited Jun 14, 2024 by avibootz
...