Write a program to count the number of triangles that can be formed from an array.
For example :
The given array is: 6 18 9 7 10
The number of possible triangles that can be formed from the array is: 5
Input Format:
The input contains an integer represents the size and the next line contains the values
Constraints:
1<=N<=100
1 <= arr[i] <= 1000
Output Format:
Print the count sample Input 1
5 6 18 9 7 10
Sample Output 1
5
Code:
#include <stdio.h>
int main()
{
int arr[100];
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++)
if (arr[i] + arr[j] > arr[k]
&& arr[i] + arr[k] > arr[j]
&& arr[k] + arr[j] > arr[i])
count++;
}
}
printf("%d",count);
return 0;
}
Code language: C/AL (cal)