Count the number of possible triangles | Practice | GeeksforGeeks

Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles. 
Example 1:
Input: 
n = 3
arr[] = {3, 5, 4}
Output: 
1
Explanation: 
A triangle is possible 
with all the elements 5, 3 and 4.
Example 2:
Input: 
n = 5
arr[] = {6, 4, 9, 7, 8}
Output: 
10
Explanation: 
There are 10 triangles
possible  with the given elements like
(6,4,9), (6,7,8),...

 
Your Task: 
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.

Expected Time Complexity: O(n2).
Expected Space Complexity: O(1).

Constraints:
3 <= n <= 103
1 <= arr[i] <= 103
0
shubham211019971 week ago
simplest solution, T(n)=O(n^2)
int pair(int arr[],int si,int ei,int x){
   int i=si,j=ei,count=0;
   while(i<j){
       int sum=arr[i]+arr[j];
       if(sum>x){
           count=count+(j-i);
           j--;
       }else{
           i++;
       }
   }
   return count;
}
int findNumberOfTriangles(int arr[], int n){
      sort(arr,arr+n);
      int count=0;
      for(int i=n-1;i>=2;i--){
          count+=pair(arr,0,i-1,arr[i]);
      }
      return count;
   }
0
achaudhary1732 weeks ago
class Solution{   //Function to count the number of possible triangles.   static int findNumberOfTriangles(int arr[], int n)   {    Arrays.sort(arr);          int count = 0;              for(int i = arr.length - 1; i >= 0; i--){                      int l = 0;           int r = i - 1;                      while(l < r){               if(arr[l] + arr[r] > arr[i]){                   count += (r - l);                   r--;               }               else{                   l++;               }           }       }       return count;          }}
  
0
wolfofsv2 weeks ago
O(n^2) time and O(1) space
int findNumberOfTriangles(int arr[], int n)
    {
        // code here
        sort(arr, arr + n);
        int ans = 0;
        for(int i = 0; i < n; i++){
            int k = i + 2;
            for(int j = i + 1; j < n; j++){
                while(k < n && arr[k] < arr[i] + arr[j]){
                    k++;
                }
                ans += k - (j + 1);
            }
        }
        return ans;
    }
0
ishubham0103 weeks ago
C++ Solution:
		int count=0;
        sort(arr, arr+n);
        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++)
                {
                    if(arr[i]+arr[j]>arr[k])
                        count++;
                    else 
                        break;
                }
            }
        }
        return count;
-2
amrit_kumar1 month ago
 int findNumberOfTriangles(int arr[], int n)
    {
        sort(arr, arr+n); // sort the array in ascending order
        int count =0; // intialize reuslt to zero
        for(int i=n-1;i>=2;i--) // start from right end 
        {
            int l =0; // left size is zero
            int r = i-1; // right side is i-1
            while(l<r)
            {
                if(arr[l]+arr[r]>arr[i]) // if condition ssatisfied increase count
                {
                   count+=r-l; // for a particular right side total count is calculated
                   r--;   // decrease the right value
                }
                else
                  l++; // if condiiton failed increase the left side
            }
        }
        return count;
    }
0
asyrovPremium1 month ago
public int CountTrianglesForSide(int[] arr, int side, int high)
{
   int count = 0;
   
   for (int low = 0; low < high; high--)
   {
       while (low < high && arr[low] + arr[high] <= side) low++;
       count += high - low;
   }
   
   return count;    
}
public int findNumberOfTriangles(int[] arr, int n)
{
   int count = 0;
   
   Array.Sort(arr);
   
   for (int i = n - 1; i > 1; i--)
       count += CountTrianglesForSide(arr, side: arr[i], high: i - 1);
       
   return count;
}
-1
ashvinkict201 month ago
// code here       int ans  =0;       //We need to check only 1 condition a,b,c are sorted       //a+b>c              sort(arr,arr+n);              //  Fix c and try to find that satisfy the  condition a+b>c       // c+a>b,c+b>a is satisfied by default if a<b<c             for(int c = n-1;c>=2;c--){           int a = 0;           int b = c-1;          
      while(b>a) {           if(arr[a]+arr[b]>arr[c]){
                   ans += b-1;
                   b--;
            }else{
                   a++;
            }       }        return ans;   }
0
ashvinkict20
This comment was deleted.
+2
abhijeet194031 month ago
An optimised solution for C++
 
int findNumberOfTriangles(int arr[], int n)   {       // code here       sort(arr,arr+n);       int count = 0;       for(int i=0;i<n-2;i++)       {           int c = 1;           for(int j=i+2;j<n;j++)           {               if((arr[i]+arr[i+1])>arr[j])               count += c;               else               {                   for(int k=i+2;k<j;k++)                   {                       if(arr[i]+arr[k]>arr[j])                       {                           count+=j-k;                           break;                       }                   }               }               c++;           }       }       return count;   }
-2
aakasshuit2 months ago
//Java Solution

        int count=0;
        Arrays.sort(arr);
        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++){
                    if(arr[i]+arr[j]>arr[k]){
                        count++;
                    }
                    else{
                        break;
                    }
                }
            }
        }
        return count;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.