Skip to content

added 3024 lc solution #1510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions dsa-solutions/lc-solutions/3000-3099/3024-Type-of-Triangle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
id: type-of-triangle
title: Type of Triangle (LeetCode)
sidebar_label: 3024-TypeOfTriangle
tags:
- Triangle
- Array
description: Determine the type of triangle that can be formed by an array of integers representing the sides of the triangle.
sidebar_position: 3024
---

## Problem Description

| Problem Statement | Solution Link | LeetCode Profile |
| :---------------- | :------------ | :--------------- |
| [Type of Triangle](https://leetcode.com/problems/type-of-triangle/) | [Type of Triangle Solution on LeetCode](https://leetcode.com/problems/type-of-triangle/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) |

## Problem Description

You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle.

A triangle is called equilateral if it has all sides of equal length.
A triangle is called isosceles if it has exactly two sides of equal length.
A triangle is called scalene if all its sides are of different lengths.
Return a string representing the type of triangle that can be formed or "none" if it cannot form a triangle.

### Example 1

- **Input:** `nums = [3,3,3]`
- **Output:** `"equilateral"`
- **Explanation:** Since all the sides are of equal length, therefore, it will form an equilateral triangle.

### Example 2

- **Input:** `nums = [3,4,5]`
- **Output:** `"scalene"`
- **Explanation:**
- `nums[0] + nums[1] = 3 + 4 = 7`, which is greater than `nums[2] = 5`.
- `nums[0] + nums[2] = 3 + 5 = 8`, which is greater than `nums[1] = 4`.
- `nums[1] + nums[2] = 4 + 5 = 9`, which is greater than `nums[0] = 3`.
Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle.
As all the sides are of different lengths, it will form a scalene triangle.

### Constraints

- `nums.length == 3`
- `1 <= nums[i] <= 100`

## Approach

To determine the type of triangle that can be formed by the given sides, we need to:

1. Check if the given sides can form a valid triangle using the triangle inequality theorem.
2. If the sides form a valid triangle, check if the triangle is equilateral, isosceles, or scalene based on the lengths of the sides.

### Solution Code

#### Python

```python
class Solution:
def triangleType(self, nums: List[int]) -> str:
a, b, c = sorted(nums)

if a + b <= c:
return "none"
if a == b == c:
return "equilateral"
if a == b or b == c or a == c:
return "isosceles"

return "scalene"
```

#### C++
```c++
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class Solution {
public:
string triangleType(vector<int>& nums) {
sort(nums.begin(), nums.end());
int a = nums[0], b = nums[1], c = nums[2];

if (a + b <= c) {
return "none";
}
if (a == b && b == c) {
return "equilateral";
}
if (a == b || b == c || a == c) {
return "isosceles";
}

return "scalene";
}
};

```

#### Java
```java
import java.util.Arrays;

class Solution {
public String triangleType(int[] nums) {
Arrays.sort(nums);
int a = nums[0], b = nums[1], c = nums[2];

if (a + b <= c) {
return "none";
}
if (a == b && b == c) {
return "equilateral";
}
if (a == b || b == c || a == c) {
return "isosceles";
}

return "scalene";
}
}

```

### Conclusion
The solutions use a simple sorting approach to determine the type of triangle that can be formed by
the given sides. This ensures that the problem is solved in a straightforward and efficient manner
across different programming languages.
Loading