✅ Java Beats 100% || Beginner Friendly ||  Easy Solution || With Comments || Winner's Solution ✅
The below solution is not mine, it is of @arignote  who ranked 9th in this contest\n\n\nimport java.util.*;\n\nclass Solution {\n\n    public List<Integer> numb
**The below solution is not mine, it is of @arignote  who ranked 9th in this contest**\n\n```\nimport java.util.*;\n\nclass Solution {\n\n    public List<Integer> numberOfAlternatingGroups(int[] colors, int[][] queries) {\n        // Initialize Fenwick Trees for count and sum operations\n        NumArray count = new NumArray(new int[colors.length + 1]);\n        NumArray sum = new NumArray(new int[colors.length + 1]);\n\n        // TreeSet to keep track of indices where alternating groups start\n        TreeSet<Integer> set = new TreeSet<>();\n        \n        // Process initial colors to populate the TreeSet and Fenwick Trees\n        for (int i = 0; i < colors.length; i++) {\n            if (colors[i] == colors[(i + 1) % colors.length]) {\n                add(i, colors.length, set, count, sum);\n            }\n        }\n\n        ArrayList<Integer> list = new ArrayList<>();\n        \n        // Process each query\n        for (int[] query : queries) {\n            if (query[0] == 1) {\n                // Query type 1: Count the number of alternating groups of size query[1]\n                list.add(set.isEmpty() ? colors.length : \n                    sum.sumRange(query[1], colors.length) - \n                    (query[1] - 1) * count.sumRange(query[1], colors.length));\n            } else if (colors[query[1]] != query[2]) {\n                // Query type 2: Update the color at index query[1]\n                colors[query[1]] = query[2];\n                \n                // Update boundaries based on the change in color\n                if (query[2] == colors[(query[1] - 1 + colors.length) % colors.length]) {\n                    add((query[1] - 1 + colors.length) % colors.length, colors.length, set, count, sum);\n                } else {\n                    remove((query[1] - 1 + colors.length) % colors.length, colors.length, set, count, sum);\n                }\n                if (query[2] == colors[(query[1] + 1) % colors.length]) {\n                    add(query[1], colors.length, set, count, sum);\n                } else {\n                    remove(query[1], colors.length, set, count, sum);\n                }\n            }\n        }\n        return list;\n    }\n\n    private void add(int x, int n, TreeSet<Integer> set, NumArray count, NumArray sum) {\n        // Add a new boundary for an alternating group\n        set.add(x);\n        if (set.size() == 1) {\n            count.update(n, 1);\n            sum.update(n, n);\n        }\n        int prev = set.lower(x) == null ? set.last() : set.lower(x);\n        int next = set.higher(x) == null ? set.first() : set.higher(x);\n        \n        // Update Fenwick Trees\n        count.update(set.size() == 2 ? n : (next - prev + n) % n, -1);\n        sum.update(set.size() == 2 ? n : (next - prev + n) % n, set.size() == 2 ? -n : -(next - prev + n) % n);\n        count.update((x - prev + n) % n, 1);\n        sum.update((x - prev + n) % n, (x - prev + n) % n);\n        count.update((next - x + n) % n, 1);\n        sum.update((next - x + n) % n, (next - x + n) % n);\n    }\n\n    private void remove(int x, int n, TreeSet<Integer> set, NumArray count, NumArray sum) {\n        // Remove an existing boundary for an alternating group\n        if (set.size() == 1) {\n            count.update(n, -1);\n            sum.update(n, -n);\n        }\n        int prev = set.lower(x) == null ? set.last() : set.lower(x);\n        int next = set.higher(x) == null ? set.first() : set.higher(x);\n        \n        // Update Fenwick Trees\n        count.update(set.size() == 2 ? n : (next - prev + n) % n, 1);\n        sum.update(set.size() == 2 ? n : (next - prev + n) % n, set.size() == 2 ? n : (next - prev + n) % n);\n        count.update((x - prev + n) % n, -1);\n        sum.update((x - prev + n) % n, -(x - prev + n) % n);\n        count.update((next - x + n) % n, -1);\n        sum.update((next - x + n) % n, -(next - x + n) % n);\n        set.remove(x);\n    }\n\n    class NumArray {\n        private int[] tree;\n        private int[] nums;\n\n        public NumArray(int[] nums) {\n            this.tree = new int[nums.length + 1];\n            this.nums = nums;\n            for (int i = 0; i < nums.length; i++) {\n                add(i + 1, nums[i]);\n            }\n        }\n\n        public void update(int index, int val) {\n            add(index + 1, val);\n            nums[index] = val;\n        }\n\n        public int sumRange(int left, int right) {\n            return prefixSum(right + 1) - prefixSum(left);\n        }\n\n        private int lowBit(int x) {\n            return x & -x;\n        }\n\n        private void add(int index, int val) {\n            while (index < tree.length) {\n                tree[index] += val;\n                index += lowBit(index);\n            }\n        }\n\n        private int prefixSum(int index) {\n            int sum = 0;\n            while (index > 0) {\n                sum += tree[index];\n                index -= lowBit(index);\n            }\n            return sum;\n        }\n    }\n}\n```\n\n**Approach in Steps**\n\n**1. Initialization:**\n\n* Create two Fenwick Trees (count and sum) to manage the count and total length of alternating groups.\n* Use a TreeSet to keep track of indices where alternating color groups start.\n\n**2. Processing Initial Colors:**\n\n* Traverse the colors array and add boundaries to the TreeSet for every alternating group. Update the Fenwick Trees accordingly.\n\n**3. Handling Queries:**\n\n* Type 1 Query: \n* For queries that require counting the number of alternating groups of a specific size, use the Fenwick Trees to get the count and sum of groups, and calculate the result based on the difference in cumulative sums.\n\n* Type 2 Query: For queries that update the color at a specific index:\n* Update the color and adjust the boundaries of the alternating groups.\nUse the add and remove methods to update the TreeSet and Fenwick Trees based on whether the new color creates or removes alternating boundaries.\n\n**4. Return Results:**\n\nAfter processing all queries, return the results for type 1 queries as a list.
['Tree', 'Java']