Minimize difference after changing all odd elements to even | Function to minimize the difference between two elements of array ; Find all the element which are possible by multiplying 2 to odd numbers ; Sort the array ; Find the minimum difference Iterate and find which adjacent elements have the minimum difference ; Print the minimum difference ; Given array ; Function call
function minDiff ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a [ i ] *= 2 ; } a . sort ( ( a , b ) => a - b ) ; let mindifference = a [ 1 ] - a [ 0 ] ; for ( let i = 1 ; i < a . length ; i ++ ) { mindifference = Math . min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } document . write ( mindifference ) ; } let arr = [ 3 , 8 , 13 , 30 , 50 ] ; let n = arr . length ; minDiff ( arr , n ) ;