Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Build a graph of n nodes where nodes are the indices of the array and edges for node i are nodes i+1, i-1, j where arr[i] == arr[j].
Start bfs from node 0 and keep distance. The answer is the distance when you reach node n-1.