给定一个整数数组arr ,任务是计算对(i, j)的数量,其中i < j使得arr[j] % arr[i] = 0 。
例子:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 5
Input: arr[] = {1, 1, 2, 2, 3, 3}
Output: 11
方法一:
迭代数组的所有对,并不断增加满足所需条件的对的计数。
下面的代码是上述方法的实现:
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
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live