Header Ads Widget

Responsive Advertisement

HackerRank problem : Preprocessor Solution in C++

 Q: https://www.hackerrank.com/challenges/preprocessor-solution/problem


Program Solution:

/* Enter your macros here */
//http://www.cplusplus.com/doc/tutorial/preprocessor/
#define FUNCTION(MINMAXfn, opr) void MINMAXfn(int &a, int &b){ a=(a)opr(b)?(a):(b);}
#define foreach(v, i) for(int i=0; i<v.size(); i++)
#define io(v) cin>>v
#define INF 10000000
#define toStr(x) #x


#include <iostream>
#include <vector>
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<int> v(n);
    foreach(v, i) {
        io(v)[i];
    }
    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;

}

Post a Comment

0 Comments