Hackerearth Bishu and Soldiers Solution

 Solution in C++:

///**********ALLAH IS ALMIGHTY************///

///AH Tonmoy

///Department of CSE,23rd batch

///Islamic University,Bangladesh  

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,i,m,a,r;
  6. vector<int>v;
  7. cin>>n;
  8. for(i=0; i<n; i++)
  9. {
  10. cin>>a;
  11. v.push_back(a);
  12. }
  13. sort(v.begin(),v.end());
  14. int s[n+1]={0};
  15. for(i=1;i<=n;i++)
  16. {
  17. s[i]=v[i-1]+s[i-1];
  18. }
  19. cin>>m;
  20. while(m--)
  21. {
  22. cin>>r;
  23. int pos=upper_bound(v.begin(),v.end(),r)-v.begin();
  24. cout<<pos<<" "<<s[pos]<<endl;
  25. }
  26. }

No comments

Most View Post

Recent post

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.