prakash951's blog

By prakash951, history, 5 hours ago, In English
Write a program to find the number of pairs of elements in an array, that satisfy the below condition.

min(∣a−b∣,∣a+b∣)≤min(∣a∣,∣b∣) 
max(∣a−b∣,∣a+b∣)≥max⁡(∣a∣,∣b∣)

min(∣a−b∣,∣a+b∣)≤min(∣a∣,∣b∣)≤max⁡(∣a∣,∣b∣)≤max(∣a−b∣,∣a+b∣)

Example:

[-9, 6, -5, 3]

-9 -5
-9 6
-5 3
-5 6
3 6

constraints:

1<=N<=200000

-1000000000<=a[i]<=1000000000

Solved it with O(NxN)

Required Complexity: O(NlogN)

If anybody has better solution please share.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
5 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by prakash951 (previous revision, new revision, compare).

»
5 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by prakash951 (previous revision, new revision, compare).

»
3 hours ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it
2 * min( |a|, |b| ) >= max( |a|, |b| )

NlogN

//vector v
int ans=0;
   for ( auto &&i : v ) { i = abs(i); }
   sort(v.begin( ), v.end( ));
   for ( int i = 0, j = 1; j < n; ++j ) {
      for ( ; i < j and (2 * v[i] < v[j]); i++ ) {}
      ans += j &mdash; i;
   }
»
7 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

Approach
1. Sort the given array in descending order with custom comparator that comapre for abs() value. So the sorted array will be [-9,6,-5,3](luckily in the given example it is already sorted accoridng to the comparator)
2.Only the first condition is neccesary because addition of two number is always greater than the numbermax(|a+b|,|a-b|) it is true for both positive and negative numbers.
3.for each number in the array we will apply Binary search on index+1-->end to avoid duplicate pairs.
4.Now start=index+1 and end=n-1 for a particular value. mid=start+(end-start)/2
5.if min(a-b)=min(a,b) it means the difference is the biggest value that can be the answer. It means every small value is also the answer beacuse we want lesser or equal.
6.if min(a-b)>min(a-b) it means the answer is in the left part of array because the difference is too big.

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define read(x) int x;cin>>x

using namespace std;
bool cmp(int a, int b){
    return abs(a) > abs(b);
}
int bs(int &num,int s,int e,vector<int> &arr){
    if(s>e) return 0;
    int mid=s+(e-s)/2;
    int curr=0;
    if(min(abs(num-arr[mid]),abs(num+arr[mid]))==min(abs(num),abs(arr[mid]))){
        curr+=mid-s+1;
    }
    else if(min(abs(num-arr[mid]),abs(num+arr[mid]))<min(abs(num),abs(arr[mid]))){
        curr+=mid-s+1+bs(num,mid+1,e,arr);
    }
    else curr+=bs(num,s,mid-1,arr);
    return curr;
}
int main(){
    fast_io;
    int t;cin>>t;
    while(t--){
        read(n);
        vector<int> arr(n);              
        rep(i,0,n) cin>>arr[i];          
        sort(arr.begin(),arr.end(),cmp);
        int cnt=0;
        rep(i,0,n){
            cnt+=bs(arr[i],i+1,n-1,arr);
        }
        cout<<cnt<<endl;
    }   
    return 0;
}

I hope it helps because i tryed my best to do it. plz tell me if my code is right or wrong.