Given two arrays **A[]** and **B[]** consisting of **N** positive integers and a integer **M**, the task is to find the minimum value of **X** such that operation **(A[i] + X) % M** performed on every element of array **A[]** results in the formation of an array with frequency of elements same as that in another given array **B[]**.

**Examples:**

Input:N = 4, M = 3, A[] = {0, 0, 2, 1}, B[] = {2, 0, 1, 1}

Output:1

Explanation:

Modifying the given array A[] to { (0+1)%3, (0+1)%3, (2+1)%3, (1+1)%3 }

= { 1%3, 1%3, 3%3, 2%3 },

= { 1, 1, 0, 2 }, which is equivalent to B[] in terms of frequency of distinct elements.

Input:N = 5, M = 10, A[] = {0, 0, 0, 1, 2}, B[] = {2, 1, 0, 0, 0}

Output:0

Explanation:

Frequency of elements in both the arrays are already equal.

**Approach:** This problem can be solved by using Greedy Approach. Follow the steps below:

- There will be at least one possible value of
**X**such that for every index**i**,**( A[i] + X ) % M = B[0]**. - Find all the possible values of
**X**that convert each element of**A[]**to the first element of**B[]**. - Check whether these possible
**X**values satisfy the other remaining values of**B[]**. - If there are multiple answers, take the minimum value of
**X**.

Below is the implementation of the above approach:

## C++

`// CPP program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utility function to find ` `// the answer ` `int` `moduloEquality(` `int` `A[], ` `int` `B[], ` ` ` `int` `n, ` `int` `m) ` `{ ` ` ` ` ` `// Stores the frequncies of ` ` ` `// array elements ` ` ` `map<` `int` `, ` `int` `> mapA, mapB; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `mapA[A[i]]++; ` ` ` `mapB[B[i]]++; ` ` ` `} ` ` ` ` ` `// Stores the possible values ` ` ` `// of X ` ` ` `set<` `int` `> possibleValues; ` ` ` ` ` `int` `FirstElement = B[0]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `int` `cur = A[i]; ` ` ` ` ` `// Generate possible positive ` ` ` `// values of X ` ` ` `possibleValues ` ` ` `.insert( ` ` ` `cur > FirstElement ` ` ` `? m - cur + FirstElement ` ` ` `: FirstElement - cur); ` ` ` `} ` ` ` ` ` `// Initialize answer ` ` ` `// to MAX value ` ` ` `int` `ans = INT_MAX; ` ` ` ` ` `for` `(` `auto` `it : ` ` ` `possibleValues) { ` ` ` ` ` `// Flag to check if the ` ` ` `// current element of the ` ` ` `// set can be considered ` ` ` `bool` `posible = ` `true` `; ` ` ` ` ` `for` `(` `auto` `it2 : mapA) { ` ` ` ` ` `// If the frequency of an element ` ` ` `// in A[] is not equal to that ` ` ` `// in B[] after the operation ` ` ` `if` `(it2.second ` ` ` `!= mapB[(it2.first + it) % m]) { ` ` ` ` ` `// Current set element ` ` ` `// cannot be considered ` ` ` `posible = ` `false` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Update minimum value of X ` ` ` `if` `(posible) { ` ` ` `ans = min(ans, it); ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `int` `m = 3; ` ` ` ` ` `int` `A[] = { 0, 0, 2, 1 }; ` ` ` `int` `B[] = { 2, 0, 1, 1 }; ` ` ` ` ` `cout << moduloEquality(A, B, n, m) ` ` ` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(N^{2})

**Auxillary Space: **O(N)

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:

- Minimize increment/decrement of Array elements to make each modulo K equal
- Smallest number to make Array sum at most K by dividing each element
- Minimum number of steps to make all elements of array equal
- Find the minimum number to be added to N to make it a prime number
- Minimum number to be added to all digits of X to make X > Y
- Find the number of operations required to make all array elements Equal
- Minimum number of increment-other operations to make all array elements equal.
- k largest(or smallest) elements in an array | added Min Heap method
- Number of ways to divide an array into K equal sum sub-arrays
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Find the minimum number to be added to N to make it a power of K
- Least number to be added to or subtracted from N to make it a Perfect Square
- Find the minimum number of operations required to make all array elements equal
- Sum of elements in an array with frequencies greater than or equal to that element
- Find element in array with frequency equal to sum of frequencies of other elements
- Check whether it is possible to make both arrays equal by modifying a single element
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Make all numbers of an array equal
- Smallest subarray from a given Array with sum greater than or equal to K

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.