Minimize difference after changing all odd elements to even | Java program for the above approach ; 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 ; Driver Code ; Given array ; Function call
import java . util . * ; class GFG { public static void minDiff ( long [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a [ i ] *= 2 ; } Arrays . sort ( a ) ; long mindifference = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < a . length ; i ++ ) { mindifference = Math . min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } System . out . println ( mindifference ) ; } public static void main ( String [ ] args ) { long [ ] arr = { 3 , 8 , 13 , 30 , 50 } ; int n = arr . length ; minDiff ( arr , n ) ; } }