You are given an array of integers `nums` and an integer `k`. Find the maximum length of a contiguous subarray such that the mathematical average of its elements is exactly equal to `k`. If no such subarray exists, return 0.
Explanation: The subarray [1, 3, 2, 6] has a sum of 12. Its length is 4, making the average 12 / 4 = 3, which equals k. This is the longest valid subarray.
Explanation: No contiguous subarray has elements that average to 4.