C++ | Beast 100% | Explained Properly | Simple Binary Search 
IntuitionThe goal is to find the smallest amount of energy to start with so that you can complete all tasks in some order.The key idea is:
Some tasks are harder
# Intuition
The goal is to find the smallest amount of energy to start with so that you can complete all tasks in some order.

The key idea is:
Some tasks are harder to start (high minimum) but don’t cost much (actual).
These tasks should be done earlier, while you still have enough energy.

# Approach
1.)Sort the tasks in descending order of (minimum - actual):
--> This ensures we handle strict/energy-demanding tasks first.
--> The intuition is: harder-to-start tasks are done early while we have high energy.
2.) Use Binary Search to find the minimum initial energy:
Search space is between 0 and a large upper bound (e.g., 1e9).
For each energy guess (mid), simulate going through tasks in sorted order:
Check if energy at every step is ≥ task’s minimum.
If yes, it's a valid starting energy → try smaller value.
If not, try a larger one.

# Complexity
- Time complexity:
  O(nlogM)

- Space complexity:
  O(1)

# Code
```cpp []
class Solution {
public:
// The logic is simple we do the task according to the order of minimum-actual means we do that task first which has more diff. We are doing this bcz the task with more diff requires more energy to finish take an example as [16,18],[2,16] if we did 1st task then we can't do 2nd but if 2nd done then we can do 1st with energy as 20
    int minimumEffort(vector<vector<int>>& tasks) {
        // Sort according to diff
        sort(tasks.begin(), tasks.end(), [](vector<int>& a, vector<int>& b){
            return a[1] - a[0] > b[1] - b[0];
        });
        // Binary search on answer 
        int l = 0, h = 1e9, ans = 0;
        while(l <= h){
            int m = l + (h - l) / 2;
            int e = m;
            bool ok = 1;
            for(auto& t : tasks){
                if(e < t[1]){
                    ok = 0;
                    break;
                }
                e -= t[0];
            }
            // if possible means we can do it in more less energy 
            if(ok){
                ans = m;
                h = m - 1;
            }else{
                l = m + 1;
            }
        }
        return ans;
    }
};

```
['Array', 'Binary Search', 'Sorting', 'C++']