Skip to main content

Command Palette

Search for a command to run...

Count Number of Teams

Published
2 min read
Count Number of Teams
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.

There are n soldiers standing in a line. Each soldier is assigned a unique rating value.

You have to form a team of 3 soldiers amongst them under the following rules:

  • Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).

  • A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).

Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).

LeetCode Problem - 1395

class Solution {
    public int numTeams(int[] rating) {
        int answer = 0; // Initialize a counter to keep track of valid teams

        // Loop through each element as the first member of the team
        for (int i = 0; i < rating.length; i++) {
            // Loop through the elements after the first member as the second member
            for (int j = i + 1; j < rating.length; j++) {
                // Loop through the elements after the second member as the third member
                for (int k = j + 1; k < rating.length; k++) {
                    // Check if the current team members form an increasing sequence
                    if (rating[i] < rating[j] && rating[j] < rating[k]) {
                        answer++; // Increment the counter for a valid team
                    }
                    // Check if the current team members form a decreasing sequence
                    if (rating[i] > rating[j] && rating[j] > rating[k]) {
                        answer++; // Increment the counter for a valid team
                    }
                }
            }
        }

        return answer; // Return the total number of valid teams
    }
}

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.