Advertisement
Iam_Sandeep

Count no Of triangles

Jun 27th, 2022
701
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None
  1. class Solution:
  2.     #Function to count the number of possible triangles.
  3.     def findNumberOfTriangles(self, arr, n):
  4.         arr.sort()
  5.         ans=0
  6.         for i in range(n-1,1,-1):
  7.             l,r=0,i-1
  8.             while l<r:
  9.                 if arr[l]+arr[r]>arr[i]:
  10.                     ans=ans+(r-l)
  11.                     r=r-1
  12.                 else:
  13.                     l=l+1
  14.         return ans
Advertisement
RAW Paste Data Copied
Advertisement