Counting the triangles that can be formed from segments of given lengths
The statement V.push_back(temp); can be inefficient, as the vector V may need to be reallocated multiple times.

Use std::vector::reserve(N) to ensure sufficient space exists in the vector before reading in the data to avoid multiple reallocations.




  \$3 \le N \le 10^7\$
  \$1 \le \mathrm{arr}[i] \le 10^3\$


With \$10^7\$ pigeons and \$10^3\$ holes, many holes can have over 10,000 pigeons!  You might want to consider an array of counts indexed by side length.  No sort required; it is automatically sorted.  But your algorithm will need considerable reworking.