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

51,875 answers

573 users

How to find a pair with maximum product from int array in VB.NET

2 Answers

0 votes
Imports System

Public Class Program
    Shared item1 As Integer = 0
    Shared item2 As Integer = 0
      
    Public Shared Function max_product_from_int_array(ByVal arr() As Integer) 
        item1 = arr(0)
        item2 = arr(1) 

        Dim len As Integer =  arr.Length

        For i As Integer = 0 To len - 1
            For  j As Integer = i + 1 To len - 1
                if (arr(i) * arr(j) > item1 * item2) Then
                    item1 = arr(i) 
                    item2 = arr(j)
                End If
            Next 
        Next
    End Function 
    
    Public Shared Sub Main()
        Dim arr() As Integer = {3, 9, 1, 3, 7, 0, 8, 4}
  
        max_product_from_int_array(arr)
 
        Console.Write("{0} {1}", item1, item2)
    End Sub
End Class



' run

' 9 8
'

 



answered Apr 15, 2019 by avibootz
edited Dec 26, 2025 by avibootz
0 votes
Imports System

Module Module1

    Public Function MaxProductPair(arr As Integer()) As (First As Integer, Second As Integer)
		If arr Is Nothing OrElse arr.Length < 2 Then
			Throw New ArgumentException("Array must contain at least two elements.")
		End If

		Dim max1 As Integer = Integer.MinValue
		Dim max2 As Integer = Integer.MinValue
		Dim min1 As Integer = Integer.MaxValue
		Dim min2 As Integer = Integer.MaxValue

		For Each x In arr

			' Track two largest values
			If x > max1 Then
				max2 = max1
				max1 = x
			ElseIf x > max2 Then
				max2 = x
			End If

			' Track two smallest values
			If x < min1 Then
				min2 = min1
				min1 = x
			ElseIf x < min2 Then
				min2 = x
			End If
		Next

		Dim prodMax As Long = CLng(max1) * max2
		Dim prodMin As Long = CLng(min1) * min2

		If prodMax >= prodMin Then
			Return (max1, max2)
		Else
			Return (min1, min2)
		End If
	End Function

	Sub Main()
		Dim arr = { 3, 9, 1, 3, 8, 0, 4 }

		Dim pair = MaxProductPair(arr)

		Console.WriteLine($"Max product pair: {pair.First}, {pair.Second}")
	End Sub



End Module


' run:
'
' Max product pair: 9, 8
'

 



answered Dec 26, 2025 by avibootz
...