how can we set an integer to infinity did not understand it
Doubt in lecture
Hey @harryson
You can set something to infinity in two ways:
-
the one sir used,
const int inf = (int)1e9
int var = inf; -
set the variable to INT_MAX(which is the maximum integer)
int var = INT_MAX;
Whenever we need to find minimum of something, it’s a good practice to set the thing initially as infinity. Now suppose you’ve to find min element in an array of size n
you can write like:
int ans = INT_MAX;
for(int i = 0; i < n; i++){
if(arr[i] < ans) ans = arr[i];
}
here setting initially to infinity is useful because we need to compare some elements and find min, and all of them will be smaller than INT_MAX, so it’s suitable.
In similar way in the question, you need to find min of q1, q2, q3, so you set them to INT_MAX initially in any of the above two ways i described.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.