Skip to main content

Command Palette

Search for a command to run...

Alternating Groups I

Published
2 min read
Alternating Groups I
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 is a circle of red and blue tiles. You are given an array of integers colors. The color of tile i is represented by colors[i]:

  • colors[i] == 0 means that tile i is red.

  • colors[i] == 1 means that tile i is blue.

Every 3 contiguous tiles in the circle with alternating colors (the middle tile has a different color from its left and right tiles) is called an alternating group.

Return the number of alternating groups.

Note that since colors represents a circle, the first and the last tiles are considered to be next to each other.

LeetCode Problem - 3216

class Solution {
    public int numberOfAlternatingGroups(int[] colors) {
        // Initialize the count of alternating groups
        int answer = 0;
        // Get the length of the input array
        int length = colors.length;

        // Check the first and last element in the array
        // Increment answer if the first element is different from the last element and the second element
        if (colors[length - 1] != colors[0] && colors[0] != colors[1]) {
            answer++;
        }

        // Check the last two elements in the array
        // Increment answer if the last element is different from the second last element and the first element
        if (colors[length - 2] != colors[length - 1] && colors[0] != colors[length - 1]) {
            answer++;
        }

        // Iterate through the array from the second element to the second last element
        for (int i = 1; i < length - 1; i++) {
            // Increment answer if the current element is different from both its neighbors
            if (colors[i] != colors[i + 1] && colors[i] != colors[i - 1]) {
                answer++;
            }
        }

        // Return the total count of alternating groups
        return answer;
    }
}

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.