HackerRank C++ Preprocessor Solution




Preprocessor directives are lines included in the code preceded by a hash sign (#). These lines are directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.



  1. #define toStr(x) #x
  2. #define io(v) cin >> v
  3. #define INF 100000000
  4. #define foreach(v, i) for(int i = 0; i < v.size(); i++)
  5. #define FUNCTION(name, op) void name(int& a, int b) { if (b op a) a = b; }
  6. #include <iostream>
  7. #include <vector>
  8. using namespace std;
  9. #if !defined toStr || !defined io || !defined FUNCTION || !defined INF
  10. #error Missing preprocessor definitions
  11. #endif
  12. FUNCTION(minimum, <)
  13. FUNCTION(maximum, >)
  14. int main(){
  15. int n; cin >> n;
  16. vector<int> v(n);
  17. foreach(v, i) {
  18. io(v)[i];
  19. }
  20. int mn = INF;
  21. int mx = -INF;
  22. foreach(v, i) {
  23. minimum(mn, v[i]);
  24. maximum(mx, v[i]);
  25. }
  26. int ans = mx - mn;
  27. cout << toStr(Result =) <<' '<< ans;
  28. return 0;
  29. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube