I was stuck in solving the following interview practice question:
I have to write a function:
int triangle(int[] A);
that given a zero-
I have got another solution to count triangles. Its time complexity is O(N**3) and takes long time to process long arrays.
Private Function solution(A As Integer()) As Integer
' write your code in VB.NET 4.0
Dim result, size, i, j, k As Integer
size = A.Length
Array.Sort(A)
For i = 0 To Size - 3
j = i + 1
While j < size
k = j + 1
While k < size
If A(i) + A(j) > A(k) Then
result += 1
End If
k += 1
End While
j += 1
End While
Next
Return result
End Function