Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.
In this post, you will find the solution for Preprocessor Solution in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To C++
C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language.
C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same.
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.
Preprocessor Solution in C++ - Hackerrank Solution
Objective
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.
#define INF 10000000 if( val == INF) { //Do something } After the preprocessor has replaced the directives, the code will be if( val == 10000000) { //Here INF is replaced by the value with which it's defined. //Do something }
You can also define function macros which have parameters.
#define add(a, b) a + b int x = add(a, b); The second statement after the preprocessor has replaced the directives will be: int x = a + b;
To know more about preprocessor directives, you can go to this link
You’re spending your afternoon at a local school, teaching kids how to code. You give them a simple task: find the difference between the maximum and minimum values in an array of integers.
After a few hours, they came up with some promising source code. Unfortunately, it doesn’t compile! Since you don’t want to discourage them, you decide to make their code work without modifying it by adding preprocessor macros.
Review the locked stub code in your editor and add the preprocessor macros necessary to make the code work.Preprocessor Solution in C++ – Hacker Rank Solution
Input Format :
The first line contains an integer, N, denoting the size of the array.
The second line contains N space-separated integers x1, x2……, xn describing the elements in the array.
Constraints :
- 1<= N <= 10^3
- -10^3 <= xi <= 10^8
Output Format :
You are not responsible for printing any output to stdout. Once the necessary preprocessor macros are written, the locked code in your editor will print a line that says Result = Z, where Z is the difference between the maximum and minimum values in the array.
Sample Input :
5 32 332 -23 -154 65
Sample Output :
Result = 486
Explanation :
332 – (- 154) = 486
Preprocessor Solution in C++ – Hacker Rank Solution
#define FUNCTION(name, operator) void name(int ¤t, int candidate){ !(current operator candidate) ? current = candidate : false; } #define foreach(v, i) for(int i = 0; i < v.size(); i++) #define io(v) cin >> v #define INF 10000000 #define toStr(S) #S /* Preprocessor Solution in C++ - Hacker Rank Solution END */ #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; }
Well I really enjoyed reading it. This tip procured by you is very helpful for proper planning.