

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the number of possible triangles in C++
We are given an array which contains the length of sides of triangles. The goal is to find the number of possible triangles that can be made by taking any three sides from that array.
We will do this by checking if the sum of any two is always > third side. If yes these three sides can make a triangle. Increment count of possible triangles that can be made.
Let’s understand with examples.
Input − arr[]= {1,2,4,5}
Output − Count of possible triangles − 1
Explanation − Sides (2,4,5) can only make a triangle as 2+4>5 & 4+5>2 & 2+5>4
Input − arr[]= {4,5,6,3,2}
Output − Count of possible triangles − 7
Explanation − Sides (4,5,6), (4,5,2),(4,5,3),(4,3,2),(5,6,3),(5,6,2) can make triangles.
Approach used in the below program is as follows
We take an integer array arr[] initialized with random positive numbers.
Function countTriangles(int arr[], int n) takes array and its length and return possible triangles that can be made.
Take initial count of triangles as 0.
Take three for loops for three sides.
Outermost loop 0<=i<n-2, inner i<j<n-1 and innermost j<k<n
For sides arr[i],arr[j],arr[k] check if they form sides of triangle.
Check arr[i] + arr[j] > arr[k] && arr[i] + arr[k] > arr[j] && arr[k] + arr[j] > arr[i] if true increment count.
Return count as result.
Example
#include <bits/stdc++.h> using namespace std; int countTriangles(int arr[], int n){ // Count of triangles int count = 0; for (int i = 0; i < n-2; i++){ for (int j = i + 1; j < n-1; j++){ for (int k = j + 1; k < n; k++){ //any two sides have sum > third if ( arr[i] + arr[j] > arr[k] && arr[i] + arr[k] > arr[j] && arr[k] + arr[j] > arr[i]) { count++; } } } } return count; } int main(){ int Arr[] = { 1,2,5,3,6,8,10 }; int len = sizeof(Arr) / sizeof(Arr[0]); cout << "count of Triangles possible : "<< countTriangles(Arr, len); return 0; }
Output
If we run the above code it will generate the following output −
count of Triangles possible : 8
- Related Questions & Answers
- Count number of right triangles possible with a given perimeter in C++
- Find number of unique triangles among given N triangles in C++
- Find the Number of Triangles After N Moves using C++
- Program to count number of possible humble matrices in Python
- Count the number of rhombi possible inside a rectangle of given size in C++
- Count of triangles with total n points with m collinear in C++
- C++ program to find the number of triangles amongst horizontal and vertical line segments
- Find the Number of Triangles Formed from a Set of Points on Three Lines using C++\n
- Area of the circumcircle of any triangles with sides given in C++
- Count the number of distinct values in MySQL?
- Count the number of masked elements in Numpy
- Count of sub-strings of length n possible from the given string in C++
- Golang Program to Count the Number of Digits in a Number
- Count Possible Decodings of a given Digit Sequence in C++
- Count number of factors of a number - JavaScript