Minimize difference after changing all odd elements to even | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE void minDiff ( vector < ll > a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a . push_back ( a [ i ] * 2 ) ; } sort ( a . begin ( ) , a . end ( ) ) ; ll mindifference = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < a . size ( ) ; i ++ ) { mindifference = min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } cout << mindifference << endl ; } int main ( ) { vector < ll > arr = { 3 , 8 , 13 , 30 , 50 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minDiff ( arr , n ) ; return 0 ; }