Skip to main content

Command Palette

Search for a command to run...

Type of Triangle II

Published
1 min read
Type of Triangle II
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 3024

class Solution {
    // Method to determine the type of triangle based on its sides
    public String triangleType(int[] nums) {
        // Check if the sides form a valid triangle using the triangle inequality theorem
        if (((nums[0]+nums[1])>nums[2]) && ((nums[1]+nums[2])>nums[0]) && ((nums[0]+nums[2])>nums[1])){
            // Sort the sides in ascending order
            Arrays.sort(nums);

            // Variable to count the number of sides that are equal
            int sidesEqual = 1;

            // Iterate through the sorted array to count equal sides
            for(int i=0; i<nums.length-1; i++){
                if(nums[i]==nums[i+1]){
                    sidesEqual++;
                }   
            }   

            // Determine the type of triangle based on the count of equal sides
            if(sidesEqual==1){
                return "scalene" ;
            }
            else if(sidesEqual==2){
                return "isosceles" ;
            }
            else{
                return "equilateral" ;
            }
        }
        else {
            // If the sides do not form a valid triangle, return "none"
            return "none";
        }
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.