human
Count of elements which are second smallest among three consecutive elements | Function to return the count of elements P [ i ] such that P [ i ] is the second smallest among P [ i 1 ] , P [ i ] and P [ i + 1 ].
To store the required answer.
Traverse from the second element to the second last element.
Return the required answer.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to count of elements which are second smallest among three consecutive elements and the other steps required.
 def countElements ( p , n ) :
	ans = 0 ;
	for i in range ( 1 , n - 1 ) :
		if ( p [ i - 1 ] > p [ i ] and p [ i ] > p [ i + 1 ] ) :
			ans += 1 ;
		elif ( p [ i - 1 ] < p [ i ] and p [ i ] < p [ i + 1 ] ) :
			ans += 1 ;
	return ans ;
if __name__ == " _ _ main _ _ " :
	p = [ 2 , 5 , 1 , 3 , 4 ] ;
	n = len ( p ) ;
	print ( countElements ( p , n ) ) ;