-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from zafitract/zafitract-patch-1
Added Maximum Perimeter Triangle Solution in C#
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Maximum Perimeter Triangle ([HackerRank Page](https://www.hackerrank.com/challenges/maximum-perimeter-triangle)) | ||
Given *n* sticks of lengths *l<sub>0</sub> ,l<sub>1</sub> , ... l<sub>n-1</sub>* , use 3 of the sticks to construct a non-[degenerate](https://en.wikipedia.org/wiki/Degeneracy_(mathematics)) triangle with the maximum possible perimeter. Then print the lengths of its sides as 3 space-separated integers in non-decreasing order. | ||
|
||
If there are several valid triangles having the maximum perimeter: | ||
|
||
1. Choose the one with the *longest maximum side* (i.e., the largest value for the longest side of any valid triangle having the maximum perimeter). | ||
2. If more than one such triangle meets the first criterion, choose the one with the *longest minimum side* (i.e., the largest value for the shortest side of any valid triangle having the maximum perimeter). | ||
3. If more than one such triangle meets the second criterion, print any one of the qualifying triangles. | ||
|
||
If no non-degenerate triangle exists, print -1. | ||
|
||
###Input Format | ||
The first line contains single integer, *n*, denoting the number of sticks. | ||
The second line contains *n* space-separated integers, *l<sub>0</sub> ,l<sub>1</sub> , ... l<sub>n-1</sub>* , describing the respective stick lengths. | ||
|
||
###Constraints | ||
* 3 <= *n* <= 50 | ||
* 1 <= *l<sub>i</sub>* <= 10<sup>9</sup> | ||
|
||
###Output Format | ||
Print 3 non-decreasing space-separated integers, *a*, *b*, and *c* (where *a* <= *b* <= *c*) describing the respective lengths of a triangle meeting the criteria in the above *Problem Statement*. | ||
|
||
If no non-degenerate triangle can be constructed, print -1. | ||
|
||
###Sample Input 0 | ||
``` | ||
5 | ||
1 1 1 3 3 | ||
``` | ||
###Sample Output 0 | ||
``` | ||
1 3 3 | ||
``` | ||
###Sample Input 1 | ||
``` | ||
3 | ||
1 2 3 | ||
``` | ||
###Sample Output 1 | ||
``` | ||
-1 | ||
``` | ||
|
||
###Explanation | ||
|
||
*Sample Case 0:* | ||
|
||
There are 2 possible unique triangles: | ||
|
||
1. (1,1,1) | ||
2. (1,3,3) | ||
|
||
The second triangle has the largest perimeter, so we print its side lengths on a new line in non-decreasing order. | ||
|
||
*Sample Case 1:* | ||
|
||
The triangle (1,2,3) is degenerate and thus can't be constructed, so we print -1 on a new line. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
class Solution | ||
{ | ||
static void Main(String[] args) | ||
{ | ||
int n = Convert.ToInt32(Console.ReadLine()); | ||
long[] s = new long[n]; | ||
string[] s_temp = Console.ReadLine().Split(' '); | ||
s = Array.ConvertAll(s_temp, long.Parse); | ||
long maxPerimeter = -1; | ||
long[] side = new long[3]; //Storing the maximum perimeter triangle's sides' length | ||
long perimeter; //Storing the triangle's perimeter for each test | ||
//Because there are 3 sides, we iterate all of their combinations in 3 nested-loops | ||
for (int i = 0; i < n; i++) | ||
{ | ||
for (int j = 0; j < n; j++) | ||
{ | ||
for (int k = 0; k < n; k++) | ||
{ | ||
if ((i != k) && (i != j) && (j != k)) | ||
{ | ||
//A triangle is non-degenerate if: | ||
//a + b > c | ||
//a + c > b | ||
//b + c > a | ||
//Where a, b, and c are the triangle's sides' length respectively | ||
if ((s[i] + s[j] > s[k]) && (s[i] + s[k] > s[j]) && (s[j] + s[k] > s[i])) | ||
{ | ||
perimeter = s[i] + s[j] + s[k]; | ||
if (perimeter > maxPerimeter) | ||
{ | ||
side = new long[] { s[i], s[j], s[k] }; | ||
maxPerimeter = perimeter; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (maxPerimeter == -1) | ||
{ | ||
Console.WriteLine(-1); | ||
} | ||
else | ||
{ | ||
Array.Sort(side); | ||
Console.WriteLine("{0} {1} {2}", side[0], side[1], side[2]); | ||
} | ||
} | ||
} |