How to count the odd and even sum of pairs in an array with VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Friend Shared Sub count_odd_and_even_sum_of_pairs(ByVal arr As Integer(), ByVal evenodd As Integer())
        Dim total_even As Integer = 0, total_odd As Integer = 0
        Dim size As Integer = arr.Length

        For i As Integer = 0 To size - 1
            If arr(i) Mod 2 = 0 Then
                total_even += 1
            Else
                total_odd += 1
            End If
        Next

        ' For all the even elements -> sum of the pair will be even
        Dim evenPairs As Integer = ((total_even * (total_even - 1)) / 2)
        
        ' For all the odd elements -> sum of the pair will be even
        evenPairs += ((total_odd * (total_odd - 1)) / 2)
        
        ' All even elements * all odd element -> sum of the pair will be odd
        Dim oddPairs As Integer = total_even * total_odd
        
        evenodd(0) = evenPairs
        evenodd(1) = oddPairs
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {1, 2, 3, 4, 5}
        Dim evenodd As Integer() = New Integer(1) {}
        
        ' 1 + 3, 1 + 5, 2 + 4, 3 + 5 = 4 Even
		    ' 1 + 2, 1 + 4, 2 + 3, 2 + 5, 3 + 4, 4 + 5 = 6 Odd
		
        count_odd_and_even_sum_of_pairs(arr, evenodd)
        
        Console.WriteLine("Total Even Sum = " & evenodd(0))
        Console.WriteLine("Total Odd Sum = " & evenodd(1))
    End Sub
End Class

 
   
   
' run:
'
' Total Even Sum = 4
' Total Odd Sum = 6
'

 



answered Jun 16, 2024 by avibootz

Related questions

...