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++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- ll n, k, i, index, sum = 0, r;
- cin >> n >> k;
- std::vector<int> v(n);
- for (int i = 0; i < n; i++) cin >> v[i];
- for (int i = 0; i < n; i++) {
- index = upper_bound(v.begin(), v.end(), v[i] + k) - v.begin();
- r = index - 1 - i;
- sum += (r * (r - 1)) / 2;
- }
- cout << sum << endl;
- }
No comments