Codeforces Round #725 (Div. 3) 1538C. Number of Pairs Solution



Explain: 

Here given condition is      l<=a[i]+a[j]<=r  

From the condition we get    a[j]<=r-a[i]

                                             l-a[i]<=a[j]

 

              from two equation we can write     l-a[i]<=a[j]<=r-a[i];

from the equation we can find the upperbound(r-a[i])-lowerbound(l-a[i])  count for all index.


Problem Link:  https://codeforces.com/problemset/problem/1538/C

Solve 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;
int main()
{
    long long  t,n,i,j,l,r,a,x,y,b,cn,u,v;
    cin>>t;
    while(t--)
    {
        cin>>n>>l>>r;
        int a[n+9];
        cn=0;
        for(i=0; i<n; i++)cin>>a[i];
        sort(a,a+n);
        for(i=0; i<n; i++)
        {
            u=l-a[i];
            v=r-a[i];
            x=lower_bound(a+i+1,a+n,u)-a;
            y=upper_bound(a+i+1,a+n,v)-a;
            cn+=abs(y-x);
        }
        cout<<cn<<endl;
    }
}



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.