➤ Problem Link : 251A. Points on Line
✅ C++ Solution :
#include <bits/stdc++.h> using namespace std; #define ll long long int ll comb(ll n) { return (n*(n-1))/2; } int main() { ll n,d; cin>>n>>d; ll arr[n]; for(int i=0;i<n;i++) cin>>arr[i]; ll x,diff,cnt=0; for(int i=0;i<n-2;i++) { x=lower_bound(arr,arr+n,d+arr[i])-arr; if(arr[x]>d+arr[i]) diff=x-i; else diff=x-i+1; if(diff>=3) cnt+=comb(diff-1); // cout<<cnt<<endl; } cout<<cnt<<endl; return 0; }
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!