I am being given the lengths of a few lines and I am required to find how many of those will form a triangle
The Input is like this
7
1 4 13 2 25 6 11
where 7 is the no. of lines and the following lines contains length of all the lines.
This is what my code looks like at its core
for(int i=0;i<n-2;i++){ //n is the total no. of lines given initially
for(int j=i+1;j<n-1;j++){
for(int k=j+1;k<n;k++){
if(l[i]+l[j]>l[k]){
noOfTriangles++;
}else{
break;
}
}
}
}
And even though the code works as expected, right now it takes time of the order of n^3 which exceeds the time limit for some inputs . Anyway I could optimise it?
EDIT: I forgot to mention I have first sorted my array before implementing nested-for loop.