Count pairs (i, j) from given array such that i < j and arr[i] > K * arr[j]
Last Updated :
13 Jun, 2022
Given an array arr[] of length N and an integer K, the task is to count the number of pairs (i, j) such that i < j and arr[i] > K * arr[j].
Examples:
Input: arr[] = {5, 6, 2, 5}, K = 2
Output: 2
Explanation: The array consists of two such pairs:
(5, 2): Index of 5 and 2 are 0, 2 respectively. Therefore, the required conditions (0 < 2 and 5 > 2 * 2) are satisfied.
(6, 2): Index of 6 and 2 are 1, 2 respectively. Therefore, the required conditions (0 < 2 and 6 > 2 * 2) are satisfied.
Input: arr[] = {4, 6, 5, 1}, K = 2
Output: 3
Naive Approach: The simplest approach to solve the problem is to traverse the array and for every index, find numbers having indices greater than it, such that the element in it when multiplied by K is less than the element at the current index.
Follow the below steps to solve the problem:
- Initialize a variable, say cnt, with 0 to count the total number of required pairs.
- Traverse the array from left to right.
- For each possible index, say i, traverse the indices i + 1 to N – 1 and increase the value of cnt by 1 if any element, say arr[j], is found such that arr[j] * K is less than arr[i].
- After complete traversal of the array, print cnt as the required count of pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getPairs( int arr[], int N, int K)
{
int count = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
if (arr[i] > K * arr[j])
count++;
}
}
cout << count;
}
int main()
{
int arr[] = { 5, 6, 2, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
getPairs(arr, N, K);
return 0;
}
|
Java
class GFG
{
static void getPairs( int arr[], int N, int K)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
if (arr[i] > K * arr[j])
count++;
}
}
System.out.print(count);
}
public static void main(String[] args)
{
int arr[] = { 5 , 6 , 2 , 5 };
int N = arr.length;
int K = 2 ;
getPairs(arr, N, K);
}
}
|
Python3
def getPairs(arr, N, K):
count = 0
for i in range (N):
for j in range (i + 1 , N):
if (arr[i] > K * arr[j]):
count + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 5 , 6 , 2 , 5 ]
N = len (arr)
K = 2
getPairs(arr, N, K)
|
C#
using System;
class GFG
{
static void getPairs( int []arr, int N, int K)
{
int count = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
if (arr[i] > K * arr[j])
count++;
}
}
Console.Write(count);
}
public static void Main(String[] args)
{
int []arr = { 5, 6, 2, 5 };
int N = arr.Length;
int K = 2;
getPairs(arr, N, K);
}
}
|
Javascript
<script>
function getPairs(arr, N, K)
{
let count = 0;
for (let i = 0; i < N; i++)
{
for (let j = i + 1; j < N; j++)
{
if (arr[i] > K * arr[j])
count++;
}
}
document.write(count);
}
let arr = [ 5, 6, 2, 5 ];
let N = arr.length;
let K = 2;
getPairs(arr, N, K);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use the concept of merge sort and then count pairs according to the given conditions. Follow the steps below to solve the problem:
- Initialize a variable, say answer, to count the number of pairs satisfying the given condition.
- Repeatedly partition the array into two equal halves or almost equal halves until one element is left in each partition.
- Call a recursive function that counts the number of times the condition arr[i] > K * arr[j] and i < j is satisfied after merging the two partitions.
- Perform it by initializing two variables, say i and j, for the indices of the first and second half respectively.
- Increment j till arr[i] > K * arr[j] and j < size of the second half. Add (j – (mid + 1)) to the answer and increment i.
- After completing the above steps, print the value of answer as the required number of pairs.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int merge( int arr[], int temp[],
int l, int m, int r, int K)
{
int i = l;
int j = m + 1;
int cnt = 0;
for ( int i = l; i <= m; i++) {
bool found = false ;
while (j <= r) {
if (arr[i] >= K * arr[j]) {
found = true ;
j++;
}
else
break ;
}
if (found) {
cnt += j - (m + 1);
j--;
}
}
int k = l;
i = l;
j = m + 1;
while (i <= m && j <= r) {
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= m)
temp[k++] = arr[i++];
while (j <= r)
temp[k++] = arr[j++];
for ( int i = l; i <= r; i++)
arr[i] = temp[i];
return cnt;
}
int mergeSortUtil( int arr[], int temp[],
int l, int r, int K)
{
int cnt = 0;
if (l < r) {
int m = (l + r) / 2;
cnt += mergeSortUtil(arr, temp,
l, m, K);
cnt += mergeSortUtil(arr, temp,
m + 1, r, K);
cnt += merge(arr, temp, l,
m, r, K);
}
return cnt;
}
void mergeSort( int arr[], int N, int K)
{
int temp[N];
cout << mergeSortUtil(arr, temp, 0,
N - 1, K);
}
int main()
{
int arr[] = { 5, 6, 2, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
mergeSort(arr, N, K);
return 0;
}
|
Java
class GFG
{
static int merge( int arr[], int temp[],
int l, int m, int r, int K)
{
int i = l;
int j = m + 1 ;
int cnt = 0 ;
for (i = l; i <= m; i++)
{
boolean found = false ;
while (j <= r)
{
if (arr[i] >= K * arr[j])
{
found = true ;
}
else
break ;
j++;
}
if (found == true )
{
cnt += j - (m + 1 );
j--;
}
}
int k = l;
i = l;
j = m + 1 ;
while (i <= m && j <= r)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= m)
temp[k++] = arr[i++];
while (j <= r)
temp[k++] = arr[j++];
for (i = l; i <= r; i++)
arr[i] = temp[i];
return cnt;
}
static int mergeSortUtil( int arr[], int temp[],
int l, int r, int K)
{
int cnt = 0 ;
if (l < r)
{
int m = (l + r) / 2 ;
cnt += mergeSortUtil(arr, temp,
l, m, K);
cnt += mergeSortUtil(arr, temp,
m + 1 , r, K);
cnt += merge(arr, temp, l,
m, r, K);
}
return cnt;
}
static void mergeSort( int arr[], int N, int K)
{
int temp[] = new int [N];
System.out.print(mergeSortUtil(arr, temp, 0 , N - 1 , K));
}
public static void main (String[] args)
{
int arr[] = { 5 , 6 , 2 , 5 };
int N = arr.length;
int K = 2 ;
mergeSort(arr, N, K);
}
}
|
Python3
def merge(arr, temp, l, m, r, K) :
i = l
j = m + 1
cnt = 0
for l in range (m + 1 ) :
found = False
while (j < = r) :
if (arr[i] > = K * arr[j]) :
found = True
else :
break
j + = 1
if (found) :
cnt + = j - (m + 1 )
j - = 1
k = l
i = l
j = m + 1
while (i < = m and j < = r) :
if (arr[i] < = arr[j]) :
temp[k] = arr[i]
k + = 1
i + = 1
else :
temp[k] = arr[j]
k + = 1
j + = 1
while (i < = m) :
temp[k] = arr[i]
k + = 1
i + = 1
while (j < = r) :
temp[k] = arr[j]
k + = 1
j + = 1
for i in range (l, r + 1 ) :
arr[i] = temp[i]
return cnt
def mergeSortUtil(arr, temp, l, r, K) :
cnt = 0
if (l < r) :
m = (l + r) / / 2
cnt + = mergeSortUtil(arr, temp, l, m, K)
cnt + = mergeSortUtil(arr, temp, m + 1 , r, K)
cnt + = merge(arr, temp, l, m, r, K)
return cnt
def mergeSort(arr, N, K) :
temp = [ 0 ] * N
print (mergeSortUtil(arr, temp, 0 , N - 1 , K))
arr = [ 5 , 6 , 2 , 5 ]
N = len (arr)
K = 2
mergeSort(arr, N, K)
|
C#
using System;
class GFG {
static int merge( int [] arr, int [] temp, int l, int m,
int r, int K)
{
int i = l;
int j = m + 1;
int cnt = 0;
for (i = l; i <= m; i++) {
bool found = false ;
while (j <= r) {
if (arr[i] >= K * arr[j]) {
found = true ;
}
else
break ;
j++;
}
if (found == true ) {
cnt += j - (m + 1);
j--;
}
}
int k = l;
i = l;
j = m + 1;
while (i <= m && j <= r)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= m)
temp[k++] = arr[i++];
while (j <= r)
temp[k++] = arr[j++];
for (i = l; i <= r; i++)
arr[i] = temp[i];
return cnt;
}
static int mergeSortUtil( int [] arr, int [] temp, int l,
int r, int K)
{
int cnt = 0;
if (l < r) {
int m = (l + r) / 2;
cnt += mergeSortUtil(arr, temp, l, m, K);
cnt += mergeSortUtil(arr, temp, m + 1, r, K);
cnt += merge(arr, temp, l, m, r, K);
}
return cnt;
}
static void mergeSort( int [] arr, int N, int K)
{
int [] temp = new int [N];
Console.WriteLine(
mergeSortUtil(arr, temp, 0, N - 1, K));
}
static public void Main()
{
int [] arr = new int [] { 5, 6, 2, 5 };
int N = arr.Length;
int K = 2;
mergeSort(arr, N, K);
}
}
|
Javascript
<script>
function merge(arr, temp, l, m, r, K)
{
let i = l;
let j = m + 1;
let cnt = 0;
for (i = l; i <= m; i++)
{
let found = false ;
while (j <= r)
{
if (arr[i] >= K * arr[j])
{
found = true ;
}
else
break ;
j++;
}
if (found == true )
{
cnt += j - (m + 1);
j--;
}
}
let k = l;
i = l;
j = m + 1;
while (i <= m && j <= r)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= m)
temp[k++] = arr[i++];
while (j <= r)
temp[k++] = arr[j++];
for (i = l; i <= r; i++)
arr[i] = temp[i];
return cnt;
}
function mergeSortUtil(arr, temp, l, r, K)
{
let cnt = 0;
if (l < r)
{
let m = parseInt((l + r) / 2, 10);
cnt += mergeSortUtil(arr, temp,
l, m, K);
cnt += mergeSortUtil(arr, temp,
m + 1, r, K);
cnt += merge(arr, temp, l, m, r, K);
}
return cnt;
}
function mergeSort(arr, N, K)
{
let temp = new Array(N);
document.write(mergeSortUtil(
arr, temp, 0, N - 1, K));
}
let arr = [ 5, 6, 2, 5 ];
let N = arr.length;
let K = 2;
mergeSort(arr, N, K);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...