C++ |  Brute force to Binary Search | TC->O(nlogn),SC->O(n) |Commented
\n//Brute Force\nYou must have to undernstand brute force ,if you want to fully get the observation of my binary search solution.\noptimize one will become very
```\n//Brute Force\nYou must have to undernstand brute force ,if you want to fully get the observation of my binary search solution.\noptimize one will become very easy ,so get idea about brute fce and jump to optimized one.\n\nint maxTwoEvents(vector<vector<int>>&a) {\n         \n       int n=a.size();\n        int res=INT_MIN;\n        int mx=INT_MIN;\n        for(int i=0;i<n;i++)\n        {\n            vector<int>x=a[i];\n            int s1=x[0];\n            int e1=x[1];\n            int val1=x[2];\n            for(int j=0;j<n;j++)\n            {\n                if(j!=i)\n                {\n                    vector<int>v1=a[j];\n                    int s2=v1[0];\n                    int e2=v1[1];\n                    int val2=v1[2];\n                    if(s2>e1)\n                    {\n                        \n                        res=max(res,val1+val2);\n                        \n                    }\n                }\n               \n            }\n            mx=max(mx,val1);\n        }\n        return max(mx,res);\n    }\n\tNow think to optimise the above aproach,just we want to remove the repetetive work\n\twe know if we want to get in a range of values in sorted array,we can use binary search\n\tso below is the idea\n\t\n//https://leetcode.com/problems/two-best-non-overlapping-events/\n//sorting based on start time\n//starting time is equal than we will keep the smaller end time first\nstatic bool comp(vector<int>&a,vector<int>&b)\n{\n   if(a[0]==b[0])\n   return a[1]<b[1];\n   return a[0]<b[0];\n}\nint maxTwoEvents(vector<vector<int>>&a)\n{\n         \n        int n=a.size();\n        int res=INT_MIN;\n        int mx=INT_MIN;\n        sort(a.begin(),a.end(),comp);\n//here I am using maxright to store the max valuefrom the right\n//suppose we landup at middle value and found the starting point greater than end point,so according to logic we want to move our right to mid+1 but \n//There may be the case that we have some starting point after mid which is having value greater than the current mid index\n//so to avoid this conflicts we will store mid max value from the right\n//because we know that after mid all the starting point is going to be greater right\n        \n        vector<int>maxfromright(n);\n        maxfromright[n-1]=a[n-1][2];\n        for(int i=n-2;i>=0;i--)\n        {\n            maxfromright[i]=max(a[i][2],maxfromright[i+1]);\n        }\n        for(int i=0;i<n;i++)\n        {\n            //keep all three values\n            vector<int>x=a[i];\n            int s1=x[0];\n            int e1=x[1];\n            int val1=x[2];\n            //start of binary search\n            int l=i+1;\n            int r=n-1;\n            while(l<=r)\n            {\n                int mid=l+(r-l)/2;\n                vector<int>v1=a[mid];\n                //keep all three values\n                 int s2=v1[0];\n                 int e2=v1[1];\n                 int val2=v1[2];\n                 //start dividing array to get the ans\n                 int idx=-1;\n                if(s2>e1)\n                {\n                    idx=mid;\n                    r=mid-1;\n                  \n                }\n                else\n                {\n                    l=mid+1;\n                }\n                if(idx!=-1)\n                {\n                    res=max(res,val1+maxfromright[idx]);\n                }\n              \n            }\n//why this,this is because we can take only one events also \n//supose tow events are not giving max but signle events may be max so will keep track of that also\n\n            mx=max(mx,val1);\n        }\n        return max(mx,res);\n}\nTime complexity->O(nlogn)\nSpace complexity->O(n)\n```
['Array', 'Sorting', 'Binary Tree']