How to calculate inverse of modulo (remainder) operator?

57 Views Asked by At

I am running a C++ program with two arraysvec and nums which are related as:

vec[(i + k) % (nums.size())] = nums[i]

Edit: Writing it in math: $y = (x + k)\mod n$ Where $k$ is a constant smaller than the size of both arrays (which are equal) and $i$ is a variable iterating over both arrays. $n$ is a constant (size of array). x is the independent variable and y is the dependent variable. I asked, what would x be equal to in terms of $y$, $k$ and $n$ ?

I wanted to ask, given the above relation between vec and nums what would be the inverse of this relation? That is:

if vec[i] = nums[x];

what would the value of $x$ be in terms of $i, k$ and nums.size()?

This is a math question regarding inverting a relation with a modulo in it, and not a programming question. Hence, I posted it here. Thanks in advance.

1

There are 1 best solutions below

0
On

We have $i \pmod{n}\equiv (x+k) \pmod{n}$ where we just have to solve for $x$.

By doing a subtraction, we have $x \pmod{n} \equiv i-k \pmod{n}$.

vec[i] = nums[i-k];

If you perform a circular shift by $k$ steps to the right of nums to get vec, to reverse the process, we perform $k$ steps circular shift to the left of vec to get back nums.