给定一个大小为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).
朴素的方法:最简单的方法是从给定的数组中生成所有可能的对,对于每一对,检查是否满足要求的条件。对于满足条件的对,将count增加1 。检查所有对后,打印获得的计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:为了优化上述方法,想法是按升序对数组进行排序,然后对于每个数组元素arr[i] ,使用二分搜索找到刚好大于K的元素的索引,例如高* arr[i] 。 [i + 1, high – 1]范围内的所有元素将与arr[i]形成一对。
请按照以下步骤解决问题:
- 初始化一个变量,比如ans,以存储所需数量的对。
- 按升序对给定数组进行排序。
- 使用变量遍历数组arr[] ,比如i
- 将值2 * arr[i]的上限的索引存储在一个变量中,比如high 。
- 通过添加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.
Javascript
输出:
3
时间复杂度: O(N*log(N))
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live