给定一个由N个整数组成的数组arr [] ,任务是从该数组中打印出所有相邻的整数对,这些对在给定数组中出现多次。如果数组包含多个这样的对,请按排序顺序打印所有对。
例子:
Input: arr[] = {1, 2, 5, 1, 2}
Output:
1 2
Explanation:
1 2 is the only repeating integer pair in the array.
Input: arr[] = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2}
Output:
1 2
2 3
3 4
4 1
Explanation:
Since the array has more than one repeating pair, all the pairs are printed in the sorted order.
方法:最简单的方法是遍历数组并将每个相邻对存储在Map中。打印所有频率大于1的这样的对。请按照以下步骤解决问题:
- 创建一个Map M以将所有相邻对存储在一个数组中。
- 遍历给定数组,并将每个相邻对存储在Map M中。
- 完成上述步骤后,遍历该图,如果任意一对频率至少为一个,则将其插入向量V中。
- 按升序对向量v进行排序,并打印存储在其中的所有对。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print adjacent pairs
// in sorted order
void repeated_pairs(int arr[], int N)
{
// Store the frequency of all
// the adjacent pairs
map, int> m;
// Stores the resultant pairs
vector > v;
int i, j;
// Stores the count of all
// adjacent pairs
for (i = 0; i < N - 1; i++) {
pair p
= { arr[i], arr[i + 1] };
// Increment the count of pair
m[p]++;
}
// Store pairs that appears more
// than once
for (auto i = m.begin();
i != m.end(); i++) {
// If frequency is at least 1
if (i->second > 1) {
pair p = i->first;
// Insert pair into vector
v.push_back(p);
}
}
// Sort the vector
sort(v.begin(), v.end());
// Print the pairs
for (i = 0; i < v.size(); i++) {
pair p = v[i];
// Print the pair
cout << p.first << " "
<< p.second << endl;
}
}
// Driver Code
int main()
{
// Given arr[]
int arr[] = { 1, 2, 3, 4, 1,
2, 3, 4, 1, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
repeated_pairs(arr, N);
return 0;
}
Java
// Java code of above approach
import java.util.*;
import java.lang.*;
class pair
{
int first, second;
pair(int f, int s)
{
this.first = f;
this.second = s;
}
@Override
public boolean equals(Object obj)
{
// if both the object references are
// referring to the same object.
if(this == obj)
return true;
if(obj == null || obj.getClass() != this.getClass())
return false;
// type casting of the argument.
pair p = (pair) obj;
return (p.first == this.first && p.second == this.second);
}
@Override
public int hashCode()
{
return this.first + this.second/2;
}
}
class GFG {
// Function to print adjacent pairs
// in sorted order
static void repeated_pairs(int arr[], int N)
{
// Store the frequency of all
// the adjacent pairs
Map m=new HashMap<>();
// Stores the resultant pairs
ArrayList v = new ArrayList<>();
int i, j;
// Stores the count of all
// adjacent pairs
for (i = 0; i < N - 1; i++)
{
pair p = new pair(arr[i], arr[i + 1]);
// Increment the count of pair
m.put(p,m.getOrDefault(p, 0) + 1);
}
// Store pairs that appears more
// than once
for (Map.Entry k: m.entrySet())
{
// If frequency is at least
if (k.getValue() > 1)
{
// Insert pair into vector
v.add(k.getKey());
}
}
// Sort the vector
Collections.sort(v, (a, b)->a.first-b.first);
// Print the pairs
for (pair k:v)
{
// Print the pair
System.out.println(k.first + " " + k.second);
}
}
// Driver code
public static void main(String[] args)
{
// Given arr[]
int arr[] = { 1, 2, 3, 4, 1,
2, 3, 4, 1, 2 };
int N = arr.length;
// Function call
repeated_pairs(arr, N);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to print adjacent pairs
# in sorted order
def repeated_pairs(arr, N):
# Store the frequency of all
# the adjacent pairs
m = {}
# Stores the resultant pairs
v = []
# Stores the count of all
# adjacent pairs
for i in range(N - 1):
p = (arr[i], arr[i + 1])
# Increment the count of pair
m[p] = m.get(p, 0) + 1
# Store pairs that appears more
# than once
for i in m:
# If frequency is at least 1
if (m[i] > 1):
p = i
# Insert pair into vector
v.append(p)
# Sort the vector
v = sorted(v)
# Print the pairs
for i in range(len(v)):
p = v[i]
# Print the pair
print(p[0], p[1])
# Driver Code
if __name__ == '__main__':
# Given arr[]
arr = [ 1, 2, 3, 4, 1,
2, 3, 4, 1, 2 ]
N = len(arr)
# Function call
repeated_pairs(arr, N)
# This code is contributed by mohit kumar 29
输出:
1 2
2 3
3 4
4 1
时间复杂度: O(N * log N)
辅助空间: O(N)