Skip to content

Commit

Permalink
Added Valid Triangle Ques
Browse files Browse the repository at this point in the history
  • Loading branch information
vk-2501 committed Oct 2, 2021
1 parent 0bd32cd commit 0dcd02c
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public int triangleNumber(int[] nums) {
Arrays.sort(nums);
int c=0;

//1.Sort the array
//2. To make triangle we check sum of 2 sides is greater thena 3rd side or not

for(int i=nums.length-1;i>=0;i--){
int l=0,r=i-1;
//i have fixed l ,r and i
//If sum of l+r is coming greater than last element then obviously elements inside l and r will also be able to make triangles
while(l<r){
if(nums[l]+nums[r]>nums[i]){
c=c+(r-l);
r--;
}
// if sum is coming lesser then do l++
else{
l++;
}
}
}

return c;
}
}

0 comments on commit 0dcd02c

Please sign in to comment.