Only 1 test case not passing Please help.
Activity Selection problem
Hello @arsh_goyal,
This is a standard greedy problem. In this, you first need to sort the vector you have taken input acc. to end time and then find maximum number of activity u can select. Sorting is very important otherwise u wont get desired output.
Possible issue:
eg. for vector={(3,5),(1,2)}, ur code will give output=1 whereas answer is 2 which we get after sorting the array first.
Please, share your code if it does not works.
Give a like if this helps.
I am sharing the code
#include #include using namespace std; bool compare(pair<int,int> a, pair<int ,int > b){ int ans1=a.second-a.first; int ans2=b.second-b.first; return ans1<ans2; } int main() { int t; cin>>t; while(t–){ int n; cin>>n; pair<int,int> arr[n]; for(int i=0;i<n;i++){ cin>>arr[i].first>>arr[i].second; } int count=0; sort(arr,arr+n,compare); for(int i=0;i<n;i++){ if(arr[i].second<=arr[i+1].first){ count++; } } cout<<count<<endl; } return 0; }
Hey @arsh_goyal,
Please, share it through the online Coding Blocks IDE.
The way you have shared it may introduce many syntax errors to it. Moreover, it degrades the readability of the code.
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.