在给定数组的相同索引处交换元素后可能的唯一数组的计数
给定两个数组arr1[]和arr2[]的大小为N的不同元素。任务是在交换两个数组的相同索引处的元素后计算可能组合的总数,这样两个数组中都没有重复执行操作。
例子:
Input: arr1[] = {1, 2, 3, 4}, arr2[] = {2, 1, 4, 3}, N = 4
Output: 4
Explanation: Possible combinations of arrays are:
- {1, 2, 3, 4} and {2, 1, 4, 3}
- {2, 1, 3, 4} and {1, 2, 4, 3}
- {1, 2, 4, 3} and {2, 1, 3, 4}
- {2, 1, 4, 3} and {1, 2, 3, 4}
The bold ones are swapped elements. So, total number of combinations = 4.
Input: arr1[] = {3, 6, 5, 2, 1, 4, 7}, arr2[] = {1, 7, 2, 4, 3, 5, 6}, N = 7
Output: 8
方法:想法是为每个元素迭代数组并进行交换,然后查找当前元素的交换,需要多少额外的交换才能使数组没有重复。将每个不同的组合算作一个组(组),即对于每个组,有两种可能性进行交换或不进行交换,因此答案将是 2 的总和,乘以组数的幂。请按照以下步骤解决问题:
- 创建一个无序映射以将两个数组的元素存储在键值对中
- 取一个变量说count来表示可能的组合计数,还取一个向量来跟踪元素 say访问过。
- 遍历地图并检查元素是否未被访问,每次创建一个集合并运行一个循环,直到当前索引不等于i 。在每次迭代中,将地图当前索引的元素插入集合中,同时更新当前索引。将集合中的所有元素标记为已访问。
- 每次迭代后,在制作组(设置)时,将计数乘以2 ,因为每组交换或不交换元素有两种可能性。
- 最后,返回count 。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to count possible combinations
// of arrays after swapping of elements
// such that there are no duplicates
// in the arrays
int possibleCombinations(int arr1[], int arr2[], int N)
{
// Create an unordered_map
unordered_map mp;
// Traverse both the arrays and
// store the elements of arr2[]
// in arr1[] element index in
// the map
for (int i = 0; i < N; i++) {
mp[arr1[i]] = arr2[i];
}
// Take a variable for count of
// possible combinations
int count = 1;
// Vector to keep track of already
// swapped elements
vector visited(N + 1, 0);
for (int i = 1; i <= N; i++) {
// If the element is not visited
if (!visited[i]) {
// Create a set
set s;
// Variable to store the current index
int curr_index = i;
// Iterate a loop till curr_index
// is equal to i
do {
// Insert the element in the set
// of current index in map
s.insert(mp[curr_index]);
// Assign it to curr_index
curr_index = mp[curr_index];
} while (curr_index != i);
// Iterate over the set and
// mark element as visited
for (auto it : s) {
visited[it] = 1;
}
count *= 2;
}
}
return count;
}
// Driver Code
int main()
{
int arr1[] = { 3, 6, 5, 2, 1, 4, 7 };
int arr2[] = { 1, 7, 2, 4, 3, 5, 6 };
int N = sizeof(arr1) / sizeof(arr1[0]);
cout << possibleCombinations(arr1, arr2, N);
return 0;
}
Java
// Java implementation for the above approach
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
class GFG
{
// Function to count possible combinations
// of arrays after swapping of elements
// such that there are no duplicates
// in the arrays
public static int possibleCombinations(int arr1[], int arr2[], int N)
{
// Create an unordered_map
HashMap mp = new HashMap();
// Traverse both the arrays and
// store the elements of arr2[]
// in arr1[] element index in
// the map
for (int i = 0; i < N; i++) {
mp.put(arr1[i], arr2[i]);
}
// Take a variable for count of
// possible combinations
int count = 1;
// Vector to keep track of already
// swapped elements
int[] visited = new int[N + 1];
Arrays.fill(visited, 0);
for (int i = 1; i <= N; i++) {
// If the element is not visited
if (visited[i] <= 0) {
// Create a set
HashSet s = new HashSet();
// Variable to store the current index
int curr_index = i;
// Iterate a loop till curr_index
// is equal to i
do {
// Insert the element in the set
// of current index in map
s.add(mp.get(curr_index));
// Assign it to curr_index
curr_index = mp.get(curr_index);
} while (curr_index != i);
// Iterate over the set and
// mark element as visited
for (int it : s) {
visited[it] = 1;
}
count *= 2;
}
}
return count;
}
// Driver Code
public static void main(String args[]) {
int arr1[] = { 3, 6, 5, 2, 1, 4, 7 };
int arr2[] = { 1, 7, 2, 4, 3, 5, 6 };
int N = arr1.length;
System.out.println(possibleCombinations(arr1, arr2, N));
}
}
// This code is contributed by gfgking.
Python3
# Python3 implementation for the above approach
# Function to count possible combinations
# of arrays after swapping of elements
# such that there are no duplicates
# in the arrays
def possibleCombinations(arr1, arr2, N) :
# Create an unordered_map
mp = {};
# Traverse both the arrays and
# store the elements of arr2[]
# in arr1[] element index in
# the map
for i in range(N) :
mp[arr1[i]] = arr2[i];
# Take a variable for count of
# possible combinations
count = 1;
# Vector to keep track of already
# swapped elements
visited = [0]*(N + 1);
for i in range(1 , N + 1) :
# If the element is not visited
if (not visited[i]) :
# Create a set
s = set();
# Variable to store the current index
curr_index = i;
# Iterate a loop till curr_index
# is equal to i
while True :
# Insert the element in the set
# of current index in map
s.add(mp[curr_index]);
# Assign it to curr_index
curr_index = mp[curr_index];
if (curr_index == i) :
break
# Iterate over the set and
# mark element as visited
for it in s :
visited[it] = 1;
count *= 2;
return count;
# Driver Code
if __name__ == "__main__" :
arr1 = [ 3, 6, 5, 2, 1, 4, 7 ];
arr2 = [ 1, 7, 2, 4, 3, 5, 6 ];
N = len(arr1);
print(possibleCombinations(arr1, arr2, N));
# This code is contributed by AnkThon
C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to count possible combinations
// of arrays after swapping of elements
// such that there are no duplicates
// in the arrays
public static int
possibleCombinations(int[] arr1, int[] arr2, int N)
{
// Create an unordered_map
Dictionary mp
= new Dictionary();
// Traverse both the arrays and
// store the elements of arr2[]
// in arr1[] element index in
// the map
for (int i = 0; i < N; i++) {
mp[arr1[i]] = arr2[i];
}
// Take a variable for count of
// possible combinations
int count = 1;
// Vector to keep track of already
// swapped elements
int[] visited = new int[N + 1];
for (int i = 1; i <= N; i++) {
// If the element is not visited
if (visited[i] <= 0) {
// Create a set
HashSet s = new HashSet();
// Variable to store the current index
int curr_index = i;
// Iterate a loop till curr_index
// is equal to i
do {
// Insert the element in the set
// of current index in map
s.Add(mp[curr_index]);
// Assign it to curr_index
curr_index = mp[curr_index];
} while (curr_index != i);
// Iterate over the set and
// mark element as visited
foreach(int it in s) { visited[it] = 1; }
count *= 2;
}
}
return count;
}
// Driver Code
public static void Main(string[] args)
{
int[] arr1 = { 3, 6, 5, 2, 1, 4, 7 };
int[] arr2 = { 1, 7, 2, 4, 3, 5, 6 };
int N = arr1.Length;
Console.WriteLine(
possibleCombinations(arr1, arr2, N));
}
}
// This code is contributed by ukasp.
Javascript
输出
8
时间复杂度: O(N 2 )
辅助空间: O(N)