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

51,843 answers

573 users

How to count the number of possible triangles from a given array in VB.NET

1 Answer

0 votes
Imports System

' Triangle = the sum of any two values (sides) > than the third value (third side)

' A + B > C
' B + C > A
' C + A > B

Public Class Program
    Public Shared Function CountTriangles(ByVal arr As Integer()) As Integer
        Dim size As Integer = arr.Length
        Dim count As Integer = 0

        For i As Integer = 0 To size - 1
           	For j As Integer = i + 1 To size - 1
                For k As Integer = j + 1 To size - 1
                    If arr(i) + arr(j) > arr(k) AndAlso arr(i) + arr(k) > arr(j) AndAlso arr(k) + arr(j) > arr(i) Then
                        count += 1
                        Console.Write(arr(i) & " + " & arr(j) & " > " & arr(k) & " | ")
                        Console.Write(arr(i) & " + " & arr(k) & " > " & arr(j) & " | ")
                        Console.WriteLine(arr(k) & " + " & arr(j) & " > " & arr(i))
                    End If
                Next
            Next
        Next

        Return count
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {120, 80, 13, 16, 9, 14, 19}

        Dim total_triangles As Integer = CountTriangles(arr)

        Console.Write("Total triangles : " & total_triangles)
    End Sub
End Class




' run:
'
' 13 + 16 > 9 | 13 + 9 > 16 | 9 + 16 > 13
' 13 + 16 > 14 | 13 + 14 > 16 | 14 + 16 > 13
' 13 + 16 > 19 | 13 + 19 > 16 | 19 + 16 > 13
' 13 + 9 > 14 | 13 + 14 > 9 | 14 + 9 > 13
' 13 + 9 > 19 | 13 + 19 > 9 | 19 + 9 > 13
' 13 + 14 > 19 | 13 + 19 > 14 | 19 + 14 > 13
' 16 + 9 > 14 | 16 + 14 > 9 | 14 + 9 > 16
' 16 + 9 > 19 | 16 + 19 > 9 | 19 + 9 > 16
' 16 + 14 > 19 | 16 + 19 > 14 | 19 + 14 > 16
' 9 + 14 > 19 | 9 + 19 > 14 | 19 + 14 > 9
' Total triangles : 10
' 

 



answered Apr 14, 2023 by avibootz
...