human
Can you provide a code to duplicates in an array in O ( n ) time and by using O ( 1 ) extra space and the other steps required given the following:
Duplicates in an array in O ( n ) time and by using O ( 1 ) extra space | Set | Function to find repeating elements.
Flag variable used to represent whether repeating element is found or not ..
Check if current element is repeating or not . If it is repeating then value will be greater than or equal to n ..
Check if it is first repetition or not . If it is first repetition then value at index arr [ i ] is less than 2 * n . Print arr [ i ] if it is first repetition ..
Add n to index arr [ i ] to mark presence of arr [ i ] or to mark repetition of arr [ i ] ..
If flag variable is not set then no repeating element is found . So print - 1..
Driver Function"
How can the above be solved in Python?
assistant
#Here is code to duplicates in an array in O ( n ) time and by using O ( 1 ) extra space and the other steps required.
 def printDuplicates ( arr , n ) :
	fl = 0 ;
	for i in range ( 0 , n ) :
		if ( arr [ arr [ i ] % n ] >= n ) :
			if ( arr [ arr [ i ] % n ] < 2 * n ) :
				print ( arr [ i ] % n , end = " ▁ " )
				fl = 1 ;
		arr [ arr [ i ] % n ] += n ;
	if ( fl == 0 ) :
		print ( " - 1" )
arr = [ 1 , 6 , 3 , 1 , 3 , 6 , 6 ] ;
arr_size = len ( arr ) ;
printDuplicates ( arr , arr_size ) ;