human
Check if array elements are consecutive | Added Method 3 | The function checks if the array elements are consecutive . If elements are consecutive , then returns true , else returns false.
1 ) Get the Minimum element in array.
2 ) Get the Maximum element in array.
3 ) Max - Min + 1 is equal to n , then only check all elements.
Create a temp array to hold visited flag of all elements . Note that , calloc is used here so that all values are initialized as false.
If we see an element again , then return false.
If visited first time , then mark the element as visited.
If all elements occur once , then return true.
if ( Max - Min + 1 != n ).
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to check if array elements are consecutive and the other steps required.
 def areConsecutive ( arr , n ) :
	if ( n < 1 ) :
		return False
	Min = min ( arr )
	Max = max ( arr )
	if ( Max - Min + 1 == n ) :
		visited = [ False for i in range ( n ) ]
		for i in range ( n ) :
			if ( visited [ arr [ i ] - Min ] != False ) :
				return False
			visited [ arr [ i ] - Min ] = True
		return True
	return False
arr = [ 5 , 4 , 2 , 3 , 1 , 6 ]
n = len ( arr )
if ( areConsecutive ( arr , n ) == True ) :
	print ( " Array ▁ elements ▁ are ▁ consecutive ▁ " )
else :
	print ( " Array ▁ elements ▁ are ▁ not ▁ consecutive ▁ " )