Answers for "count the number of possible triangles"

C++
0

count the number of possible triangles

#include<bits/stdc++.h>
using namespace std;

int count_triangles(int arr[], int n)
{
  int ans = 0;
  for (int i = n - 1; i >= 2; i--){
    int l = 0, r = i - 1;
    while(l < r){
      if (arr[l] + arr[r] > arr[i]){
        ans += r - l;
        r--;
      }
      else l++;
  }
  
  return ans;
}
Posted by: Guest on July-30-2021

Code answers related to "count the number of possible triangles"

Browse Popular Code Answers by Language