Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
910 views
in Online Assessments by Expert (34,270 points) | 910 views

2 Answers

0 like 0 dislike
Approach:
5 pairs of (i,j): {(2,4), (2,5), (4,5), (3,5),(3,4)} satisfying the given inequality. Therefore, the answer is 5.

My approach:
Brute force, O(N^2):

for(int i=0;i<n-1;i++){
    for(int j=i+1;j<n;j++){
        if(ai-aj+c <= bi-bj+d)
           count++;
by Expert (34,270 points)
0 like 0 dislike
Q1. Count Pairs
Given 2 integer arrays, a and b and 2 integers c and d, find the pairs of i,j such that
(ai - aj+c) <= (bi - bj + d), where i<j.
Sample Test Case:
Given:
n = 5
c = 3
d = 2
Array a = [9,4,2,3,6]
Array b = [1,4,3,1,2]
by Expert (34,270 points)