Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code
function minSum ( arr , n ) { let sum = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { let j = i ; while ( j < n && arr [ j ] <= arr [ j - 1 ] ) { arr [ j ] = arr [ j ] + 1 ; j ++ ; } } sum = sum + arr [ i ] ; } return sum ; } let arr = [ 2 , 2 , 3 , 5 , 6 ] ; let n = arr . length ; document . write ( minSum ( arr , n ) ) ;