We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

define FUNCTION(min_max, signal) void calc_##min_max(int& a, int b) {a = a signal b ? a : b;}

/*a is passed by ref while b is passed by val || calc_##min_max , concatenation operator in preprocessor->> '##' -> if signal is < then it becomes calc_minimum /*

include

include

using namespace std;

if !defined toStr || !defined io || !defined FUNCTION || !defined INF

error Missing preprocessor definitions

endif

FUNCTION(minimum, <)
FUNCTION(maximum, >)

int main(){
int n; cin >> n;
vector v(n);
**foreach(v, i) {
io(v,i); ** /slightly modified as the prev given format was having few problem/
}
int mn = INF;
int mx = -INF;
foreach(v, i) {
minimum(mn, v[i]);
maximum(mx, v[i]);
}
int ans = mx - mn;
cout << toStr(Result =) <<' '<< ans;
return 0;

}

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

## Preprocessor Solution

You are viewing a single comment's thread. Return to all comments →

## C++

**#define INF 1000000

## define minimum(a,b) a=(ausing ternary op/

## define maximum(a,b) a=(a>b)?a:b

## define foreach(v,i) for(int i=0;i

## define io(v,i) cin>>v[i] /

defining input/## define toStr(res) #res /*stringify operator-> # */

## define FUNCTION(min_max, signal) void calc_##min_max(int& a, int b) {a = a signal b ? a : b;}

/*a is passed by ref while b is passed by val || calc_##min_max , concatenation operator in preprocessor->> '##' -> if signal is < then it becomes calc_minimum

/*## include

## include

using namespace std;

## if !defined toStr || !defined io || !defined FUNCTION || !defined INF

## error Missing preprocessor definitions

endifFUNCTION(minimum, <) FUNCTION(maximum, >)

int main(){ int n; cin >> n; vector v(n); **foreach(v, i) { io(v,i); ** /

slightly modified as the prev given format was having few problem/ } int mn = INF; int mx = -INF; foreach(v, i) { minimum(mn, v[i]); maximum(mx, v[i]); } int ans = mx - mn; cout << toStr(Result =) <<' '<< ans; return 0;}