给定一个表示手套长度的N 个整数数组arr[] ,任务是从给定数组中计算最大可能的手套对。请注意,手套只能与相同尺寸的手套配对,并且只能是一对的一部分。
例子:
Input: arr[] = {6, 5, 2, 3, 5, 2, 2, 1}
Output: 2
(arr[1], arr[4]) and (arr[2], arr[5]) are the only possible pairs.
Input: arr[] = {1, 2, 3, 1, 2}
Output: 2
简单的方法:对给定的数组进行排序,使所有相等的元素彼此相邻。现在,遍历数组,对于每个元素,如果它等于它旁边的元素,那么它是一个有效对并跳过这两个元素。否则当前元素不会与任何其他元素形成有效对,因此只会跳过当前元素。
下面是上述方法的实现:
C++14
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum
// possible pairs of gloves
int cntgloves(int arr[], int n)
{
// To store the required count
int count = 0;
// Sort the original array
sort(arr, arr + n);
for (int i = 0; i < n - 1;) {
// A valid pair is found
if (arr[i] == arr[i + 1]) {
count++;
// Skip the elements of
// the current pair
i = i + 2;
}
// Current elements doesn't make
// a valid pair with any other element
else {
i++;
}
}
return count;
}
// Driver code
int main()
{
int arr[] = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << cntgloves(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG {
// Function to return the maximum
// possible pairs of gloves
static int cntgloves(int arr[], int n)
{
// Sort the original array
Arrays.sort(arr);
int res = 0;
int i = 0;
while (i < n) {
// take first number
int number = arr[i];
int count = 1;
i++;
// Count all duplicates
while (i < n && arr[i] == number) {
count++;
i++;
}
// If we spotted number just 2
// times, increment
// result
if (count >= 2) {
res = res + count / 2;
}
}
return res;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {6, 5, 2, 3, 5, 2, 2, 1};
int n = arr.length;
// Function call
System.out.println(cntgloves(arr, n));
}
}
// This code is contributed by Lakhan murmu
Python3
# Python3 implementation of the approach
# Function to return the maximum
# possible pairs of gloves
def cntgloves(arr, n):
# To store the required count
count = 0
# Sort the original array
arr.sort()
i = 0
while i < (n-1):
# A valid pair is found
if (arr[i] == arr[i + 1]):
count += 1
# Skip the elements of
# the current pair
i = i + 2
# Current elements doesn't make
# a valid pair with any other element
else:
i += 1
return count
# Driver code
if __name__ == "__main__":
arr = [6, 5, 2, 3, 5, 2, 2, 1]
n = len(arr)
# Function call
print(cntgloves(arr, n))
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the maximum
// possible pairs of gloves
static int cntgloves(int[] arr, int n)
{
// To store the required count
int count = 0;
// Sort the original array
Array.Sort(arr);
for (int i = 0; i < n - 1;) {
// A valid pair is found
if (arr[i] == arr[i + 1]) {
count++;
// Skip the elements of
// the current pair
i = i + 2;
}
// Current elements doesn't make
// a valid pair with any other element
else {
i++;
}
}
return count;
}
// Driver code
public static void Main(String[] args)
{
int[] arr = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = arr.Length;
// Function call
Console.WriteLine(cntgloves(arr, n));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
2
有效的方法
1)创建一个空的哈希表(unordered_map在C ++,Java中的HashMap,字典在Python)
2) 存储所有元素的频率。
3)遍历哈希表。对于每个元素,找到它的频率。将每个元素的结果增加频率/2,
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。