#K271. Longest Equal Subarray
Longest Equal Subarray
Longest Equal Subarray
You are given an array of integers. Your task is to find the length of the longest contiguous subarray in which all elements are equal. More formally, given an array (arr) of (n) integers, determine the maximum value of (k) such that there exists an index (i) satisfying (arr[i] = arr[i+1] = \cdots = arr[i+k-1]). This problem tests your ability to iterate through an array and maintain a counter for consecutive equal elements.
For example, if the input array is [1, 2, 2, 3, 3, 3, 2, 2, 2, 2]
, the answer is 4
because the longest subarray of equal elements is [2, 2, 2, 2]
.
inputFormat
The input is given from standard input (stdin) and consists of two lines:
- The first line contains a single integer (n) representing the number of elements in the array. (n) can be zero.
- The second line contains (n) space-separated integers representing the elements of the array. If (n = 0), the second line may be omitted.
outputFormat
Output a single integer to standard output (stdout), which is the length of the longest contiguous subarray with all elements equal.## sample
10
1 2 2 3 3 3 2 2 2 2
4