How to count the total pairs in an array in all permutations with even and odd products in VB.NET

1 Answer

0 votes
Imports System

Friend Class Program
    Friend Shared Function countOddProductPairs(ByVal arr As Integer()) As Integer
        Dim odd As Integer = 0

        For i As Integer = 0 To arr.Length - 1
            If arr(i) Mod 2 <> 0 Then
                odd += 1
            End If
        Next

        Return odd * (odd - 1)
    End Function

    Friend Shared Function countEvenProductPairs(ByVal total_odd_product_pairs As Integer, ByVal arr As Integer()) As Integer
        Dim total_pairs As Integer = arr.Length * (arr.Length - 1)
        
        Return total_pairs - total_odd_product_pairs
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {1, 8, 5}
        
        ' The pairs are: (1, 8), (1, 5), (8, 1), (8, 5), (5, 1), (5, 8) 
        ' Pairs with Even product: (1, 8), (8, 1), (8, 5), (5, 8) = 4
        ' Total pairs with Odd product: (1, 5), (1, 5) = 2
        
        Dim total_odd_product_pairs As Integer = countOddProductPairs(arr)
        Dim total_even_product_pairs As Integer = countEvenProductPairs(total_odd_product_pairs, arr)
        
        Console.WriteLine("Total Even Product Pairs = " & total_even_product_pairs)
        Console.WriteLine("Total Odd Product Pairs= " & total_odd_product_pairs)
    End Sub
End Class


 
   
   
' run:
'
' Total Even Product Pairs = 4
' Total Odd Product Pairs= 2
'

 



answered Jun 18, 2024 by avibootz
...