Help me in solving BROKPHON problem

My issue

help me building this logic

My code

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here

	}
}

Problem Link: Broken Telephone Practice Coding Problem - CodeChef

@harikrishna029
here . refer my c++ code for better understanding of the logic

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    int a[n];
	    int ans=0;
	    map<int,int> mp;
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	    }
	    for(int i=1;i<n;i++)
	    {
	        if(a[i]!=a[i-1])
	        {
	            if(mp[i-1]==0)
	            {
	                mp[i-1]=1;
	                ans++;
	            }
	            ans++;
	            mp[i]=1;
	        }
	    }
	    cout<<ans<<endl;
	}
	return 0;
}