JavaScript Program to Count Reverse Pairs
Last Updated :
14 May, 2024
Given an array, array [] of N integers, find the count of reverse pairs. A pair of indices (i, j) is said to be a reverse pair if both the following conditions are met:
0 <= i < j < N
arr[i] > 2 * arr[j]
Examples:
Input: N = 6,
arr = [3, 2, 4, 5, 1, 20]
Output: 3
Explanation: The reverse pairs are:
(0, 4), arr[0] = 3 and arr[4] = 1, 3 > 2(1)
(2, 4), arr[2] = 4 and arr[4] = 1, 4 > 2(1)
(3, 4), arr[3] = 5 and arr[4] = 1, 5 > 2(1)
Input: N = 5,
arr = [2, 4, 3, 5, 1]
Output: 3
Explanation: The reverse pairs are:
(1, 4), arr[1] = 4 and arr[4] = 1, 4 > 2(1)
(2, 4), arr[2] = 3 and arr[4] = 1, 3 > 2(1)
(3, 4), arr[3] = 5 and arr[4] = 1, 5 > 2(1)
Using Nested Loops
In this approach, we iterate through each element in the array and compare it with subsequent elements to find reverse pairs, where the element at a higher index is smaller than the element at a lower index. The counter is incremented whenever such a pair is encountered.
Example: The below example uses Nested Loops to count reverse pairs using JavaScript.
JavaScript
let cnt = 0;
const arr = [2, 4, 3, 5, 1]
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > 2 * arr[j]) {
cnt++;
}
}
}
console.log(cnt);
Time Complexity: O(n^2), where n is the number of elements in the array.
Space Complexity: O(1)
Using Merge Sort
In this approach, we recursively divide the array into smaller parts using the merge sort algorithm. During the merge step, while merging two sorted arrays, we count the reverse pairs by comparing elements from different parts. This allows us to efficiently count reverse pairs in the array.
Example: The below example uses Merge Sort to count reverse pairs using JavaScript.
JavaScript
let cnt = 0;
const arr = [3, 2, 4, 5, 1, 20];
function countReversePairs(arr) {
if (arr.length <= 1) {
return arr;
}
// Divide the array into two halves
const mid = Math.floor(arr.length / 2);
// Recursively sort left half
const left = countReversePairs(arr.slice(0, mid));
// Recursively sort right half
const right = countReversePairs(arr.slice(mid));
let i = 0;
let j = 0;
// Merge and count reverse pairs
while (i < left.length && j < right.length) {
if (left[i] > 2 * right[j]) {
cnt += left.length - i;
j++;
} else {
i++;
}
}
// Merge sorted left and right halves
return left.concat(right).sort((a, b) => a - b);
}
countReversePairs(arr);
console.log(cnt);
Time Complexity: O(nlogn), where n is the number of elements in the array.
Space Complexity: O(n)
Similar Reads
JavaScript Program Count number of Equal Pairs in a String In this article, we are going to learn how can we count a number of equal pairs in a string. Counting equal pairs in a string involves finding and counting pairs of consecutive characters that are the same. This task can be useful in various applications, including pattern recognition and data analy
3 min read
JavaScript Program to Construct an Array from its pair-sum Array The pair-sum array is a unique construction that holds the sum of all potential pairs of elements from the original array. At first glance, it might appear to be challenging, but in this article, we'll Construct an array from its pair-sum array and discover some of its most intriguing uses. What is
4 min read
Javascript Program to Count pairs with given sum Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'.Note: Duplicate pairs are also allowed. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5},
2 min read
Java Program to Reverse a List Reversing a list means after reversing the list, the first element should be swapped with the last element, the second element should be swapped with the second last element, and so on till the middle element and the resultant list is the reversed list. Example: Input: "PLATFORM", "LEARNING", "BEST"
3 min read
Reverse Number Program in Java In Java, reversing a number means that the digit at the first position should be swapped with the last digit, the second digit will be swapped with the second last digit, and so on, till the middle element.Example of reversing a number:Input: n = 357Output: 753Input n = 100Output: 1 ( leading zeros
3 min read