给定大小为N的数组arr [] , 和一个整数K ,任务是计算给定数组中对的数目,该对中较大的元素除以较小的元素对的对的商不超过K。
例子:
Input: arr[] = {3, 12, 5, 13}, K=2
Output: 2
Explanation: Pairs satisfying the given conditions are (3, 5) and (12, 13).
Input: arr[] = {2, 3, 9, 5}, K=2
Output: 3
Explanation: Pairs satisfying the given conditions are (2, 3), (3, 5) and (5, 9).
天真的方法:最简单的方法是从给定的数组中生成所有可能的对,并针对每个对检查是否满足所需条件。对于满足条件的对,将计数增加1 。检查所有对之后,打印获得的计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,我们的想法是按升序对数组进行排序,然后针对每个数组元素arr [i] ,使用Binary Search查找刚好大于K的元素的索引(即高) * arr [i] 。 [i +1,high – 1]范围内的所有元素都将与arr [i]成对。
请按照以下步骤解决问题:
- 初始化一个变量,例如ans,以存储所需的对数。
- 以升序对给定数组进行排序。
- 使用变量遍历数组arr [] ,说我
- 将值2 * arr [i]的上限索引存储在一个变量中,例如high 。
- 通过添加高– i – 1来更新ans的值。
- 打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number
// having quotient of division
// of larger element by the smaller
// element in the pair not exceeding K
void countPairs(int arr[], int n, int k)
{
// Sort the array in ascending order
sort(arr, arr + n);
// Store the required result
int ans = 0;
// Traverse the array
for (int i = 0; i < n - 1; i++) {
// Store the upper bound for
// the current array element
int high
= upper_bound(arr, arr + n, k * arr[i]) - arr;
// Update the number of pairs
ans += high - i - 1;
}
// Print the result
cout << ans;
}
// Driver Code
int main()
{
// Given array, arr[]
int arr[] = { 2, 3, 9, 5 };
// Store the size of the array
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
countPairs(arr, n, k);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
public static int upper_bound(int arr[], int key)
{
int l = -1, r = arr.length;
while (l + 1 < r)
{
int m = (l + r) >>> 1;
if (arr[m] <= key)
l = m;
else
r = m;
}
return l + 1;
}
// Function to count the number
// having quotient of division
// of larger element by the smaller
// element in the pair not exceeding K
static void countPairs(int arr[], int n, int k)
{
// Sort the array in ascending order
Arrays.sort(arr);
// Store the required result
int ans = 0;
// Traverse the array
for(int i = 0; i < n - 1; i++)
{
// Store the upper bound for
// the current array element
int high = upper_bound(arr, k * arr[i]);
// Update the number of pairs
ans += high - i - 1;
}
// Print the result
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Given array, arr[]
int arr[] = { 2, 3, 9, 5 };
// Store the size of the array
int n = arr.length;
int k = 2;
countPairs(arr, n, k);
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
from bisect import bisect_right
# Function to count the number
# having quotient of division
# of larger element by the smaller
# element in the pair not exceeding K
def countPairs(arr, n, k) :
# Sort the array in ascending order
arr.sort()
# Store the required result
ans = 0
# Traverse the array
for i in range(n - 1):
# Store the upper bound for
# the current array element
high = bisect_right(arr, k * arr[i])
# Update the number of pairs
ans += high - i - 1
# Prthe result
print(ans)
# Driver Code
# Given array, arr[]
arr = [ 2, 3, 9, 5 ]
# Store the size of the array
n = len(arr)
k = 2
countPairs(arr, n, k)
# This code is contributed by sanjoy_62.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
static int upper_bound(int []arr, int key)
{
int l = -1, r = arr.Length;
while (l + 1 < r)
{
int m = (l + r) >> 1;
if (arr[m] <= key)
l = m;
else
r = m;
}
return l + 1;
}
// Function to count the number
// having quotient of division
// of larger element by the smaller
// element in the pair not exceeding K
static void countPairs(int []arr, int n, int k)
{
// Sort the array in ascending order
Array.Sort(arr);
// Store the required result
int ans = 0;
// Traverse the array
for(int i = 0; i < n - 1; i++)
{
// Store the upper bound for
// the current array element
int high = upper_bound(arr, k * arr[i]);
// Update the number of pairs
ans += high - i - 1;
}
// Print the result
Console.WriteLine(ans);
}
// Driver Code
public static void Main()
{
// Given array, arr[]
int []arr = { 2, 3, 9, 5 };
// Store the size of the array
int n = arr.Length;
int k = 2;
countPairs(arr, n, k);
}
}
// This code is contributed by SURENDRA_GANGWAR.
输出:
3
时间复杂度: O(N * log(N))
辅助空间: O(1)