Minimum array elements to be changed to make Recaman 's sequence | C # 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
using System ; using System . Collections . Generic ; class GFG { static int recamanGenerator ( int [ ] arr , int n ) { arr [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == temp ) temp < 0 ) { temp = arr [ i - 1 ] + i ; break ; } } arr [ i ] = temp ; } return 0 ; } static int recamanArray ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; int [ ] recaman = new int [ n + 1 ] ; recamanGenerator ( recaman , n ) ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( recaman [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . Contains ( arr [ i ] ) ) s . Remove ( arr [ i ] ) ; } return s . Count ; } static void Main ( ) { int [ ] arr = { 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 } ; int n = arr . Length ; Console . Write ( recamanArray ( arr , n ) ) ; } }