📜  计算乘积小于 k 的有序数组中的对

📅  最后修改于: 2022-05-13 01:57:49.367000             🧑  作者: Mango

计算乘积小于 k 的有序数组中的对

给定一个排序的整数数组和数字 k,任务是计算数组中乘积小于 x 的对。
例子:

这个问题的一个简单解决方案是运行两个循环来生成所有对,并一个一个地检查当前对的乘积是否小于 x。
这个问题的一个有效解决方案是在 l 和 r 变量中获取索引的初始值和最后一个值。考虑以下两种情况:

上面的问题类似于在一个总和小于 x 的有序数组中计数对,唯一不同的是找到对的乘积而不是求和。
下面是解决这个问题的算法:

1) Initialize two variables l and r to find the candidate 
   elements in the sorted array.
       (a) l = 0
       (b) r = n - 1
2) Initialize : result = 0
2) Loop while l < r.

    // If current left and current
    // right have product smaller than x,
    // the all elements from l+1 to r
    // form a pair with current
    (a) If (arr[l] * arr[r] < x)  
          result = result + (r - l)    
          l++;  
   
    (b) Else
            r--;
       
3) Return result

下面是上述算法的实现:

C++
// C++ program to find number of pairs with
// product less than k in a sorted array
#include 
using namespace std;
 
// Function to count the pairs
int fun(int A[], int n, int k)
{
    // count to keep count of
    // number of pairs with product
    // less than k
    int count = 0;
    int i = 0;
    int j = n - 1;
 
    // Traverse the array
    while (i < j) {
 
        // If product is less than k
        // then count that pair
        // and increment 'i'
        if (A[i] * A[j] < k) {
            count += (j - i);
            i++;
        }
 
        // Else decrement 'j'
        else {
            j--;
        }
    }
 
    // Return count of pairs
    return count;
}
 
// Driver code
int main()
{
 
    int A[] = { 2, 3, 4, 6, 9 };
    int n = sizeof(A) / sizeof(int);
    int k = 20;
    cout << "Number of pairs with product less than "
         << k << " = " << fun(A, n, k) << endl;
 
    return 0;
}


Java
// Java program to find number
// of pairs with product less
// than k in a sorted array
class GFG
{
 
// Function to count the pairs
static int fun(int A[],
               int n, int k)
{
    // count to keep count of
    // number of pairs with
    // product less than k
    int count = 0;
    int i = 0;
    int j = n - 1;
 
    // Traverse the array
    while (i < j)
    {
 
        // If product is less than
        // k then count that pair
        // and increment 'i'
        if (A[i] * A[j] < k)
        {
            count += (j - i);
            i++;
        }
 
        // Else decrement 'j'
        else
        {
            j--;
        }
    }
 
    // Return count of pairs
    return count;
}
 
// Driver code
public static void main(String args[])
{
    int A[] = {2, 3, 4, 6, 9};
    int n = A.length;
    int k = 20;
     
    System.out.println("Number of pairs with " +
                     "product less than 20 = " +
                                  fun(A, n, k));
}
}
 
// This code is contributed
// by Kirti_Mangal


Python
# Python program to find number of pairs with
# product less than k in a sorted array
 
def fun(A, k):
    # count to keep count of number
    # of pairs with product less than k
    count = 0
    n = len(A)
    # Left pointer pointing to leftmost part
    i = 0
     
    # Right pointer pointing to rightmost part
    j = n-1
     
    # While left and right pointer don't meet
    while i < j:
        if A[i]*A[j] < k:
            count += (j-i)
            # Increment the left pointer
            i+= 1
        else:
            # Decrement the right pointer
            j-= 1
    return count
 
# Driver code to test above function
A = [2, 3, 4, 6, 9]
k = 20
print("Number of pairs with product less than ",
k, " = ", fun(A, k))


C#
// C# program to find number
// of pairs with product less
// than k in a sorted array
using System;
 
class GFG
{
 
// Function to count the pairs
static int fun(int []A,
               int n, int k)
{
    // count to keep count of
    // number of pairs with
    // product less than k
    int count = 0;
    int i = 0;
    int j = n - 1;
 
    // Traverse the array
    while (i < j)
    {
 
        // If product is less than
        // k then count that pair
        // and increment 'i'
        if (A[i] * A[j] < k)
        {
            count += (j - i);
            i++;
        }
 
        // Else decrement 'j'
        else
        {
            j--;
        }
    }
 
    // Return count of pairs
    return count;
}
 
// Driver code
public static void Main()
{
    int []A = {2, 3, 4, 6, 9};
    int n = A.Length;
    int k = 20;
     
    Console.WriteLine("Number of pairs with " +
                    "product less than 20 = " +
                                 fun(A, n, k));
}
}
 
// This code is contributed
// by Subhadeep


PHP


Javascript


输出:
Number of pairs with product less than 20 = 6

时间复杂度: O(N)