Finding number of arrays such that first and last elements are same with unequal adjacent elements.

42 Views Asked by At

The first and last elements of the array are the same. The size of the array is n and the array element can have values in [1, m]. How to find the number of such arrays with the condition that adjacent elements are unequal?

Feels like it can be solved using the inclusion-exclusion principle, But I'm not able to formulate it. Can anyone help?