Given an array A[] of n integers, the task is to find the number of subarrays whose minimal and maximum elements are same.
Example 1:
Input : Arr[] = {1, 1, 3}
Output : 4
Explanation:
The subarrays are
(1), (1), (3) and (1, 1) 
Example 2:
Input : Arr[] = {1, 2, 3, 4}
Output : 4
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function numberOfways() that takes an array (arr), sizeOfArray (n) and return the number of subarrays which follow above condition. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{5}

Difficulty: EASY
Tags: Data structures

Mathematics
Skill types: Data structures
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).

Starter code:
#User function Template for python3

class Solution:
    def numberOfways(self, a, n): 
        # Coplete the function

Solutions:
class Solution:

	def numberOfways(self, a, n):
		output = 1
		count = 1
		for i in range(n - 1):
			if a[i] == a[i + 1]:
				count += 1
			else:
				count = 1
			output += count
		return output


class Solution:

	def numberOfways(self, a, n):
		if n == 1:
			return 1
		ans = 0
		i = 0
		while i < n - 1:
			c = 1
			if a[i] == a[i + 1]:
				while i < n - 1 and a[i] == a[i + 1]:
					c += 1
					i += 1
			ans += c * (c + 1) // 2
			i += 1
		if a[-1] != a[-2]:
			ans += 1
		return ans


class Solution:

	def numberOfways(self, a, n):
		count = 1
		prev_var = a[0]
		answer = 0
		for i in range(1, len(a)):
			if a[i] == prev_var:
				count += 1
			else:
				answer += count * (count + 1) // 2
				count = 1
				prev_var = a[i]
		answer += count * (count + 1) // 2
		return answer


class Solution:

	def numberOfways(self, a, n):
		lis = []
		count = 1
		for i in range(1, n):
			if a[i - 1] == a[i]:
				count += 1
			else:
				lis.append(count)
				count = 1
		lis.append(count)
		res = 0
		for i in lis:
			res += i * (i + 1) // 2
		return res


class Solution:

	def numberOfways(self, a, n):
		c = 1
		s = 0
		for i in range(n - 1):
			if a[i] == a[i + 1]:
				c += 1
			else:
				s += c * (c + 1) // 2
				c = 1
		if n > 1 and a[-1] == a[-2]:
			s += c * (c + 1) // 2
		else:
			s += 1
		return s


class Solution:

	def numberOfways(self, a, n):
		c = 1
		ans = 1
		for i in range(1, n):
			if arr[i - 1] == arr[i]:
				c += 1
				ans += c
			else:
				ans += 1
				c = 1
		return ans


class Solution:

	def numberOfways(self, a, n):
		cnt = 0
		chain = 1
		for i in range(n):
			cnt += 1
			if i > 0 and a[i] == a[i - 1]:
				chain += 1
			else:
				cnt += chain * (chain + 1) // 2 - chain
				chain = 1
		cnt += chain * (chain + 1) // 2 - chain
		return cnt


class Solution:

	def numberOfways(self, a, n):
		ans = 1
		count = 1
		for i in range(1, n):
			if arr[i - 1] == arr[i]:
				count += 1
				ans += count
			else:
				ans += 1
				count = 1
		return ans
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ob = Solution()
	print(ob.numberOfways(arr, n))


class Solution:

	def numberOfways(self, a, n):
		if n == 1:
			return 1
		res = 0
		l = 0
		r = 0
		while r < n - 1:
			if arr[r] == arr[r + 1]:
				j = r
				count = 0
				s = arr[r]
				while j < n and s == arr[j]:
					count += 1
					j += 1
				res += count * (count + 1) // 2
				if j < n:
					r = j - 1
				else:
					return res
			else:
				res += 1
			r += 1
		if arr[-1] != arr[-2]:
			res += 1
		return res
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ob = Solution()
	print(ob.numberOfways(arr, n))


class Solution:

	def numberOfways(self, a, n):
		summ = n
		(count, summ1) = (0, 0)
		for i in range(n - 1):
			if a[i] == a[i + 1]:
				count += 1
			else:
				summ1 = count * (count + 1) // 2
				summ += summ1
				count = 0
		summ1 = count * (count + 1) // 2
		summ += summ1
		return summ


class Solution:

	def numberOfways(self, l, n):
		i = 0
		j = 0
		ans = 0
		while j < n:
			value = l[j]
			ans += j - i + 1
			j += 1
			while j < n and value == l[j]:
				ans += j - i + 1
				j += 1
			i = j
		return ans