17(April) Count Pairs in an Array
17. Count Pairs in an Array
The problem can be found at the following link: Question Link
Problem Description
Given an array arr
of n
integers, count all pairs (arr[i], arr[j])
in it such that i*arr[i] > j*arr[j]
and 0 β€ i < j < n
. Note: 0-based Indexing is followed.
Example:
Input:
n = 4
arr[] = {8, 4, 2, 1}
Output:
2
Explanation: If we see the array after operations [0*8, 1*4, 2*2, 3*1] => [0, 4, 4, 3]
, pairs which hold the condition i*arr[i] > j*arr[j]
are (4,1) and (2,1), so in total 2 pairs are available.
My Approach
Pairs Calculation:
Iterate through the array
arr
and calculatei*arr[i]
for each elementarr[i]
.Store the calculated values in a vector
v
.
Sorting:
Create a copy vector
temp
of vectorv
.Sort the vector
temp
in non-decreasing order.
Counting Pairs:
Iterate over the elements of the original array
arr
in reverse order.For each element
v[i]
, find the count of elements greater thanv[i]
in the sorted vectortemp
using binary search.Subtract this count from the total size of
temp
and add it to the answer.Remove the element
v[i]
from the sorted vectortemp
.
Return:
Return the final count as the answer.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(n*log(n)), as it involves sorting the vector and binary search operations.
Expected Auxiliary Space Complexity: O(n), as we use additional space to store the calculated values in vectors.
Code (C++)
class Solution {
public:
int countPairs(int arr[], int n) {
std::vector<int> v;
for (int i = 0; i < n; i++) {
v.push_back(i * arr[i]);
}
std::vector<int> temp = v;
std::sort(temp.begin(), temp.end());
int ans = 0;
for (int i = n - 1; i >= 0; i--) {
int x = std::upper_bound(temp.begin(), temp.end(), v[i]) - temp.begin();
int y = std::lower_bound(temp.begin(), temp.end(), v[i]) - temp.begin();
ans = ans + (temp.size() - x);
temp.erase(temp.begin() + y);
}
return ans;
}
};
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated