Counting Triangles

You are given an array of N non-negative integers, A0, A1 ,…, AN-1.
Considering each array element Ai as the edge length of some line segment, count the number of triangles which you can form using these array values.


  1. You can use any value only once while forming each triangle. Order of choosing the edge lengths doesn’t matter. Any triangle formed should have a positive area.

  2. Return answer modulo 109 + 7.

For example,

A = [1, 1, 1, 2, 2]

Return: 4
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Counting Triangles on Interview Code Editor
Sign Up
to access hints and editorial solutions for Counting Triangles
Click here to start solving coding interview questions