Given an array **arr1** containing integers from **1..N** and another array **arr2** containing integers from **1..M**. Each element of **arr1** is mapped to more than one element in **arr2**. The task is to maximize the number of one to one mappings possible between both arrays.

Here one to one mappings means a unique element of **arr1** must be mapped to unique element of **arr2**.

Mapping is given in the form of vector of pairs **V** where **V.first()** denotes elements of **arr1** and **V.second()** denotes elements of **arr2**.

**Examples:**

Input:N = 3, M = 3 arr1[] = {1, 2, 3} arr2[] = {1, 2, 3} V = { {1, 1}, {2, 1}, {2, 2}, {3, 1}, {3, 2}, {3, 3} }Output:3Explanation:If we carefully see here mappings are 1 --------> {1} 2 --------> {1, 2} 3 --------> {1, 2, 3} so, maximum number of unique pairings can be done as 1 --------> {1} 2 --------> {2} 3 --------> {3}Input:N = 3, M = 1 V = { {1, 1}, {2, 1}, {3, 1} };Output:1Explanation:Either 1 can be mapped or 2 or 3. so maximum one mapping is possible.

**Approach:** The **approach** used here is Greedy.

**Preprocessing step**

We are given a vector of pairs which contains mapping information in an unsorted way for eg-

so, we convert this mapping information into a vector of sets **mapping**.

For eg:

This is representing the information in one to many mapping form.

This can be easily understood by the below diagram

**Steps**:

- Find the element in arr1[] which has least number of mappings with elements of arr2[].
- Map this element to first element of its corresponding set.
- Delete all the mappings of this element.
- Also delete all the mappings which contain element from arr2[] those are already mapped.
- Repeat these four steps for all the elements of arr1[].

Below is the implementation of Above approach:

`// C++ Program to convert many to many mapping ` `// to maximum no of one to one mappings ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns the maximum number ` `// of one to one mappings between two arrays ` `int` `FindMax(vector<pair<` `int` `, ` `int` `> >& V, ` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `// Stores whether an element ` ` ` `// from the first array is used in mapping ` ` ` `bool` `* is_paired = ` `new` `bool` `[N]; ` ` ` ` ` `// Contains mapping in sorted form ` ` ` `vector<set<` `int` `> > mapping(N + 1); ` ` ` ` ` `// Initialize all the elements ` ` ` `// of first array unused ` ` ` `memset` `(is_paired, ` `sizeof` `(is_paired), ` `false` `); ` ` ` ` ` `// Insert mappings in sorted form ` ` ` `for` `(` `int` `i = 0; i < V.size(); i++) { ` ` ` `mapping[V[i].first].insert(V[i].second); ` ` ` `} ` ` ` ` ` `// While there is always at least one element ` ` ` `// which can be mapped to a unique element ` ` ` `while` `(` `true` `) { ` ` ` `int` `lowest = -1; ` ` ` ` ` `// Finds element to be mapped ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` ` ` `// There is mapping possible ` ` ` `if` `(mapping[i].size() > 0) { ` ` ` `if` `(lowest == -1) ` ` ` `lowest = i; ` ` ` `else` `if` `(mapping[i].size() ` ` ` `< mapping[lowest].size()) ` ` ` `lowest = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(lowest == -1) ` ` ` `break` `; ` ` ` ` ` `// Element used in the mapping ` ` ` `is_paired[lowest] = ` `true` `; ` ` ` ` ` `int` `remove` `= *mapping[lowest].begin(); ` ` ` ` ` `// Delete all the mappings of used element ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` ` ` `mapping[i].erase(` `remove` `); ` ` ` `if` `(i == lowest) { ` ` ` `mapping[i].clear(); ` ` ` `} ` ` ` `} ` ` ` `ans++; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Main Function ` `int` `main() ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `M = 3; ` ` ` `int` `arr1[] = { 1, 2, 3 }; ` ` ` `int` `arr2[] = { 1, 2, 3 }; ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > ` ` ` `V{ { 1, 1 }, { 2, 1 }, { 2, 2 }, { 3, 1 }, { 3, 2 }, { 3, 3 } }; ` ` ` ` ` `cout << FindMax(V, N, M) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity: ** where x is maximum no one to one mappings.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find all the possible mappings of characters in a sorted order
- Find original sequence from Array containing the sequence merged many times in order
- Minimum number of subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Convert one array to another using adjacent swaps of elements
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum prime moves to convert X to Y
- Maximum sum subarray removing at most one element
- Maximum consecutive one’s (or zeros) in a binary array
- Equally divide into two sets such that one set has maximum distinct elements
- Minimize the maximum minimum difference after one removal from array
- Maximum consecutive one’s (or zeros) in a binary circular array
- Maximize the maximum subarray sum after removing atmost one element
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Find two equal subsequences of maximum length with at least one different index
- Maximum possible GCD after replacing at most one element in the given array
- Maximum length of Strictly Increasing Sub-array after removing at most one element
- Maximum contiguous decreasing sequence obtained by removing any one element
- Find maximum meetings in one room
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.