📌  相关文章
📜  数组中对(i,j)的计数,以使arr [i]是arr [j]的因数

📅  最后修改于: 2021-05-18 00:39:35             🧑  作者: Mango

给定一个整数数组arr ,任务是计算i 的对数(i,j) ,以使arr [j]%arr [i] = 0
例子:

方法1:
遍历数组的所有对,并继续增加满足所需条件的对数。
下面的代码是上述方法的实现:

C++
// C++ Program to find
// the number of pairs
// (i, j) such that arr[i]
// is a factor of arr[j]
 
#include 
using namespace std;
 
// Function to return the
// count of Pairs
int numPairs(int arr[], int n)
{
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[j] % arr[i] == 0)
                ans++;
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 1, 2, 2, 3, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << numPairs(arr, n) << endl;
    return 0;
}


Java
// Java Program to find the number of pairs
// (i, j) such that arr[i] is a factor of arr[j]
import java.util.*;
import java.lang.*;
class GFG{
 
// Function to return the
// count of Pairs
static int numPairs(int arr[], int n)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (arr[j] % arr[i] == 0)
                ans++;
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 1, 2, 2, 3, 3 };
    int n = arr.length;
 
    System.out.println(numPairs(arr, n));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find the number
# of pairs (i, j) such that arr[i]
# is a factor of arr[j]
 
# Function to return the
# count of Pairs
def numPairs(arr, n):
 
    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
             
            if arr[j] % arr[i] == 0:
                ans += 1
 
    return ans
 
# Driver code
arr = [ 1, 1, 2, 2, 3, 3 ]
n = len(arr)
 
print(numPairs(arr, n))
 
# This code is contributed by divyamohan123


C#
// C# Program to find the number of pairs
// (i, j) such that arr[i] is a factor of arr[j]
using System;
 
class GFG{
 
// Function to return the
// count of Pairs
static int numPairs(int []arr, int n)
{
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
       for(int j = i + 1; j < n; j++)
       {
          if (arr[j] % arr[i] == 0)
              ans++;
       }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int []arr = { 1, 1, 2, 2, 3, 3 };
    int n = arr.Length;
 
    Console.Write(numPairs(arr, n));
}
}
 
// This code is contributed by Code_Mech


Javascript


C++
// C++ Program to find
// the number of pairs
// (i, j) such that arr[i]
// is a factor of arr[j]
 
#include 
using namespace std;
 
// Function to return the
// count of Pairs
int numPairs(int arr[], int n)
{
    map > mp;
    int mx = 0;
    for (int i = 0; i < n; i++) {
 
        // Update the maximum
        mx = max(mx, arr[i]);
 
        // Store the indices of
        // every element
        mp[arr[i]].push_back(i);
    }
 
    int ans = 0;
    for (auto i : mp) {
 
        int ctr = 1;
 
        // Access all indices of i
        for (int j : i.second) {
 
            // Add the number of
            // occurences of i
            // after j-th index
            ans += i.second.size() - ctr;
 
            // Traverse all multiples of i
            for (int k = 2 * i.first;
                 k <= mx;
                 k += i.first) {
 
                // Find their occurrences
                // after the j-th index
                int numGreater = 0;
                if (mp.find(k) != mp.end())
                    numGreater
                        = int(
                            mp[k]
                                .end()
                            - upper_bound(
                                  mp[k].begin(),
                                  mp[k].end(), j));
                // Add the count
                ans += numGreater;
            }
            ctr++;
        }
    }
 
    return ans;
}
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << numPairs(arr, n) << endl;
    return 0;
}


输出:
11

方法2:将所有数组元素的索引存储在映射中。遍历地图以及元素的每次出现:

  • 在当前事件之后添加相同元素的事件。
  • 使用upper_bound在当前事件之后添加所有其倍数的事件

下面的代码是上述方法的实现:

C++

// C++ Program to find
// the number of pairs
// (i, j) such that arr[i]
// is a factor of arr[j]
 
#include 
using namespace std;
 
// Function to return the
// count of Pairs
int numPairs(int arr[], int n)
{
    map > mp;
    int mx = 0;
    for (int i = 0; i < n; i++) {
 
        // Update the maximum
        mx = max(mx, arr[i]);
 
        // Store the indices of
        // every element
        mp[arr[i]].push_back(i);
    }
 
    int ans = 0;
    for (auto i : mp) {
 
        int ctr = 1;
 
        // Access all indices of i
        for (int j : i.second) {
 
            // Add the number of
            // occurences of i
            // after j-th index
            ans += i.second.size() - ctr;
 
            // Traverse all multiples of i
            for (int k = 2 * i.first;
                 k <= mx;
                 k += i.first) {
 
                // Find their occurrences
                // after the j-th index
                int numGreater = 0;
                if (mp.find(k) != mp.end())
                    numGreater
                        = int(
                            mp[k]
                                .end()
                            - upper_bound(
                                  mp[k].begin(),
                                  mp[k].end(), j));
                // Add the count
                ans += numGreater;
            }
            ctr++;
        }
    }
 
    return ans;
}
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << numPairs(arr, n) << endl;
    return 0;
}
输出:
5