human
Write me a code to check for Majority Element in a sorted array and the other steps required given the following:
Check for Majority Element in a sorted array | Python3 Program to check for majority element in a sorted array.
get last index according to n ( even or odd ).
search for first occurrence of x in arr [ ].
check if x is present and is present more than n / 2 times.
Driver program to check above function"
Solution in Python:
assistant
#Here is code to check for Majority Element in a sorted array and the other steps required.
 def isMajority ( arr , n , x ) :
	last_index = ( n // 2 + 1 ) if n % 2 == 0 else ( n // 2 )
	for i in range ( last_index ) :
		if arr [ i ] == x and arr [ i + n // 2 ] == x :
			return 1
arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 4 ]
n = len ( arr )
x = 4
if ( isMajority ( arr , n , x ) ) :
	print ( " % ▁ d ▁ appears ▁ more ▁ than ▁ % ▁ d ▁ times ▁ in ▁ arr [ ] " % ( x , n // 2 ) )
else :
	print ( " % ▁ d ▁ does ▁ not ▁ appear ▁ more ▁ than ▁ % ▁ d ▁ times ▁ in ▁ arr [ ] " % ( x , n // 2 ) )