This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Nice problem. Should have put a longer list to fail naive approaches. By the way, how should I suppose to take the input? I use a very ugly
1 |
|
Any better way to do this?
Log in to reply
Yeah probably should have...
I don't see any better way to take the input. You might us well take the array itself as it ain't big.
A solution in C++ (I'm using Visual Studio 2013):
First of all, we need to find the number of values in the set:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
We find that the number of values in the text file is 1 9 9 . We then write a program to find the largest triangle perimeter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
We find that the largest possible perimeter is 2 0 6 3 , formed by sticks of length 6 8 4 , 6 8 4 , 6 9 5
Problem Loading...
Note Loading...
Set Loading...
An efficent method to solve this problem is to first sort the array. Then we iterate over the list starting from the end. This will reduce the effort to check every time for valid triangle because if the sum of two smaller sides is greater than 3rd then obviously it is a valid triangle. Since the array is already sorted you can just traverse the array from end and if any triangle exists, that will the answer itself., otherwise we decrement down the list.