Minimum array elements to be changed to make Recaman 's sequence | Python3 implementation of the approach ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula ; If arr [ i - 1 ] - i is negative or already exists ; Function that returns minimum changes required ; Set to store first n Recaman numbers ; Generate and store first n Recaman numbers ; Insert first n Recaman numbers to set ; If current element of the array is present in the set ; Return the remaining number of elements in the set ; Driver code
def recamanGenerator ( arr , n ) : NEW_LINE INDENT arr [ 0 ] = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT temp = arr [ i - 1 ] - i NEW_LINE j = 0 NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( ( arr [ j ] == temp ) or temp < 0 ) : NEW_LINE INDENT temp = arr [ i - 1 ] + i NEW_LINE break NEW_LINE DEDENT DEDENT arr [ i ] = temp NEW_LINE DEDENT DEDENT def recamanArray ( arr , n ) : NEW_LINE INDENT s = dict ( ) NEW_LINE recaman = [ 0 for i in range ( n ) ] NEW_LINE recamanGenerator ( recaman , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s [ recaman [ i ] ] = s . get ( recaman [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in s . keys ( ) : NEW_LINE INDENT del s [ arr [ i ] ] NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT arr = [ 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( recamanArray ( arr , n ) ) NEW_LINE