Minimum increments by index value required to obtain at least two equal Array elements | Function to update every element adding to it its index value ; Function to check if at least two elements are equal or not ; Count the frequency of arr [ i ] ; Function to calculate the number of increment operations required ; Stores the minimum number of steps ; Driver code
function update ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] += ( i + 1 ) ; } } function check ( arr , N ) { let f = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < N ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { count ++ ; } } if ( count >= 2 ) { f = 1 ; break ; } } if ( f == 1 ) return true ; else return false ; } function incrementCount ( arr , N ) { let min = 0 ; while ( check ( arr , N ) != true ) { update ( arr , N ) ; min ++ ; } document . write ( min ) ; } let N = 3 ; let arr = [ 12 , 8 , 4 ] ; incrementCount ( arr , N ) ;