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
+1
ashivamsahu142 days ago
CPP Solution 
 sort(arr,arr+n);
      int count=0;
      for(int i=n-1;i>=2;i--)
      {
          int temp=arr[i];
          int l=0,r=i-1;
          while(l<r)
          {
              if(arr[l]+arr[r]>temp)
              {
                  count+=(r-l);
                  r--;
              }
              else
              l++;
          }
      }
      return count;
0
akhilpanwar52 weeks ago
C++
int findNumberOfTriangles(int arr[], int n)   {       // code here       int i,j,k;       sort(arr,arr+n);       int count=0;       int sum=0;       for(i=n-1;i>=2;i--){           j=0,k=i-1;           while(j<k){               if(arr[i]<arr[k]+arr[j]){                   count=count+k-j;                   k--;                   j=0;               }               else {                   j++;               }           }       }       return count;   }
0
thakursahabsingh123 weeks ago
C++ Solution,
 
 
class Solution{
public:   //Function to count the number of possible triangles.   int findNumberOfTriangles(int arr[], int n)   {       // code here       sort(arr,arr+n);      int count=0;      for(int i=n-1;i>=2;i--)      {          int temp=arr[i];          int l=0,r=i-1;          while(l<r)          {              if(arr[l]+arr[r]>temp)              {                  count+=(r-l);                  r--;              }              else              l++;          }      }      return count;
   }};
0
parapatiteja033 weeks ago
//C++ very easy solution
 
//TejaParapati  :)
 
sort(A,A+n);       int count=0;       for(int i=n-1;i>=2;i--)       {           int temp=A[i];           int l=0,r=i-1;           while(l<r)           {               if(A[l]+A[r]>temp)               {                   count+=(r-l);                   r--;               }               else               l++;           }       }       return count;
0
sg812k6ni3 weeks ago
 int findNumberOfTriangles(int arr[], int n)   {             sort(arr,arr+n);       int count=0,j,k;       for (int i=0;i<n-2;i++){           j=i+1,k=n-1;           while (j<n-1){           while ( j<k){               if (arr[i]+arr[j]>arr[k]){                   count+=k-j;                   j++;                   k=n-1;                                  }              else {                  k--;              }                                }           j++;           k=n-1;}              }       return count ;   // code here   }
0
lawbindpandey01w3 weeks ago
The idea here is to sort the array and traverse the array from i = 2 till n - 1 and  consider arr[i] as C (side of the triangle) and now initialize l = 0 and r = i - 1 and try to modify two pointer approach such that you calculate the the no_of such
(A, B) pairs 
class Solution{   public:   //Function to count the number of possible triangles.      int findPairCount(int arr[], int ic, int n){                     int l = 0, r = ic - 1;       int count = 0;       while(l < r){           bool flag = 0;           if(arr[l] + arr[r] > arr[ic]) count += r - l;                      else if(arr[l] + arr[r] <= arr[ic]){ flag = 1; l++; }                      if(!flag) r--;       }       return count;   }      int findNumberOfTriangles(int arr[], int n)   {       // code here       sort(arr, arr+n);              int count = 0;              for(int i = 2; i < n; i++){                      count += findPairCount(arr,i,n);                  }              return count;   }};
 
-7
omsingh44591 month ago
this belongs to easy category
-1
siddhantreddy1 month ago
int findNumberOfTriangles(int arr[], int n)
    {
        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 - 1; j++){
                while(k < n && arr[k] < arr[i] + arr[j]){
                    k++;
                }
                ans += k - j - 1;
            }
        }
        return ans;
    }
+2
gp0610962 months ago
TC: O(n2) Java :
class Solution{      //Function to count the number of possible triangles.   static int findNumberOfTriangles(int arr[], int n)   {       int count=0;       Arrays.sort(arr);       int one=0,left=0,right=0;       for(int i =n-1; i >= 2; i--){           one=arr[i];           count +=twosum(0,i-1,arr,one);       }       return count;   }   static int twosum(int left, int right ,int arr[] , int sum){       int count=0;       while(left<right){           if(arr[left]+arr[right]>sum) {count+=(right-left);right--;}           else left++;                  }              return count;   }}
+1
satyamshukla24982 months ago
O(N^2) Solution - Python
 
class Solution:   #Function to count the number of possible triangles.   def findNumberOfTriangles(self, arr, n):              arr.sort()              count = 0              for i in range(n-1,1,-1):       # going from right to left           l = 0           r = i - 1 #r pointing to one element before last index                      while l < r:               if arr[l] + arr[r] > arr[i]:                   count += (r-l) # since l and r are greater than i so elements between l and r will also be greater than i                   r -= 1                                  else:  # if smaller than try other numbers increase l index by 1                   l += 1                          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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.