具有一个元素为 K 乘以另一个元素的不同对的计数
给定一个数组arr[]和一个整数K,找出一个元素是另一个元素的K倍的最大对数,即arr[i]=K*arr[j] 。
例子:
Input: arr[] = {1, 2, 1, 2, 4} K = 2
Output: 2
Explanation: There are two possible ways to construct pairs: ({1, 2}, {1, 2}) and ({1, 2}, {2, 4}).
Input: a = {5, 4, 3, 2, 1} K = 2
Output: 1
Explanation: We can construct either set {1, 2} or set {2, 4}.
方法:对给定的数组arr[]进行排序,并检查数组arr [] 的所有可能对,并检查给定的(i, j) arr[i]=2*arr[j]。请按照以下步骤解决问题:
- 使用 C++ STL 中的 sort函数对数组arr[]进行排序。
- 初始化一个向量,用于保持已使用元素的计数。
- 将变量ans初始化为0以存储所有可能对的计数。
- 使用变量i遍历范围[0, N-1]并执行以下步骤:
- 使用变量j遍历范围[l, N-1]并执行以下操作:
- 如果used[j]和used[i]的值为 false并且arr[j]=K*arr[i] ,则将used[i]和used[j]的值设置为true并增加ans 1并打破循环。
- 使用变量j遍历范围[l, N-1]并执行以下操作:
- 最后,完成上述步骤后,打印ans 的值。
下面是上述方法的实现:
C++
// C++ program for the above approach.
#include
using namespace std;
// Function to find the maximum number
// of pairs.
int maxPairs(vector a, int k)
{
// Sort the array.
sort(a.begin(), a.end());
int n = a.size(), ans = 0;
// mark as used
vector used(n);
// iterate over all elements
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// if condition is satisfied,
// pair the elements
if (!used[j]
&& a[j] == k * a[i]
&& !used[i]) {
used[j] = used[i] = true;
ans++;
break;
}
}
}
return ans;
}
// Driver Code
int32_t main()
{
vector a{ 1, 2, 1, 2, 4 };
int k = 2;
cout << maxPairs(a, k);
return 0;
}
Java
// Java program for the above approach.
import java.util.Arrays;
class GFG {
// Function to find the maximum number
// of pairs.
public static int maxPairs(int[] a, int k)
{
// Sort the array.
Arrays.sort(a);
int n = a.length, ans = 0;
// mark as used
boolean[] used = new boolean[n];
// iterate over all elements
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// if condition is satisfied,
// pair the elements
if (!used[j]
&& a[j] == k * a[i]
&& !used[i]) {
used[i] = true;
used[j] = used[i];
ans++;
break;
}
}
}
return ans;
}
// Driver Code
public static void main(String args[])
{
int[] a = {1, 2, 1, 2, 4};
int k = 2;
System.out.println(maxPairs(a, k));
}
}
// This code is contributed by _saurabh_jaiswal.
Python3
# Python Program for the above approach
# Function to find the maximum number
# of pairs.
def maxPairs(a, k):
# Sort the array.
a.sort()
n = len(a)
ans = 0
# mark as used
used = [False] * n
# iterate over all elements
for i in range(0, n):
for j in range(i + 1, n):
# if condition is satisfied,
# pair the elements
if (used[j] == False and a[j] == k * a[i] and used[i] == False):
used[j] = used[j] = True
ans += 1
break
return ans
# Driver Code
a = [1, 2, 1, 2, 4]
k = 2
print(maxPairs(a, k))
# This code is contributed by _saurabh_jaiswal
C#
// C# program for the above approach.
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum number
// of pairs.
static int maxPairs(List a, int k)
{
// Sort the array.
a.Sort();
int n = a.Count, ans = 0;
// mark as used
int [] Ar = new int[n];
List used = new List(Ar);
// iterate over all elements
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// if condition is satisfied,
// pair the elements
if (used[j]==0
&& a[j] == k * a[i]
&& used[i]==0) {
used[j] = used[i] = 1;
ans++;
break;
}
}
}
return ans;
}
// Driver Code
public static void Main(){
List a = new List(){ 1, 2, 1, 2, 4 };
int k = 2;
Console.Write(maxPairs(a, k));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
2
时间复杂度: O(N^2)
空间复杂度: O(N)