this is the running code to find no of triangles using brute force technique
logic: in a triangle having sides a,b,c; then a+bc(if c is greatest side)
correct me if i am wrong.
#includeiostream
#includeconio.h
using namespace std;
int max(int a,int b,int c)
{
return ((ab?a:b)c?(ab?a:b):c);
}
sry ignore the last comment for i wanted to say -
your algo will work only when the numbers given are consecutive.
--
Amol Sharma
Final Year Student
Computer Science and Engineering
MNNIT Allahabad
http://gplus.to/amolsharma99
@anmol my algo will work even if nos are not in ascending
order .correct me if i am wrong.
On Jun 15, 7:45 am, Amol Sharma amolsharm...@gmail.com wrote:
sry ignore the last comment for i wanted to say -
your algo will work only when the numbers given are consecutive.
--
Amol Sharma
Final
@amol
True, but the question suggests they are . If they arent then it'll require
sorting the array and then binary search for largest index for which
a[index] a[i]+a[j] .. Then required triangles are index-j for looped
values i and j.
On Friday, 15 June 2012 20:15:26 UTC+5:30, Amol Sharma
Use two loops:
num_triangles = 0
for i:1 to (N-1)/2
for j:i+1 to i+jN
num_triangles += N-(i+j)
This gives values in increasing order such that cba (c,b,a being lengths
of sides of the triangle) and sum of any two sides is greater than the
third.
Consider :
How come 1,2,4 gives a triangle ?? 1+2 4.
On 14 June 2012 16:12, enchantress elaenjoy...@gmail.com wrote:
Use two loops:
num_triangles = 0
for i:1 to (N-1)/2
for j:i+1 to i+jN
num_triangles += N-(i+j)
This gives values in increasing order such that cba
My bad.. it should be this:
num_triangles = 0
for i:2 to N-2
for j:i+1 to N-1
num_triangles += i+j=N ? i-1:N-j
This gives values in increasing order such that cba (c,b,a being lengths
of sides of the triangle) and sum of any two sides is greater than the
third.