Codeforces Round #153 (Div. 1) 252C(Div 2) Or 251A. Points on Line Solution



  Problem Link:  https://codeforces.com/problemset/problem/251/A

Solution in C++:

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. #define ll long long int
  10. int main() {
  11. ios_base::sync_with_stdio(0);
  12. cin.tie(0);
  13. ll n, k, i, index, sum = 0, r;
  14. cin >> n >> k;
  15. std::vector<int> v(n);
  16. for (int i = 0; i < n; i++) cin >> v[i];
  17. for (int i = 0; i < n; i++) {
  18. index = upper_bound(v.begin(), v.end(), v[i] + k) - v.begin();
  19. r = index - 1 - i;
  20. sum += (r * (r - 1)) / 2;
  21. }
  22. cout << sum << endl;
  23. }

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.