# Reconstruct the array by replacing arr[i] with (arr[i-1]+1) % M

Given an array of N elements and an integer M. Now, the array is modified by replacing some of the array elements with -1. The task is to print the original array.

The elements in the **orginal array** are related as, for every index i, **a[i] = (a[i-1]+1)% M**.

It is guaranteed that there is one non zero value in the array.

**Examples**:

Input: arr[] = {5, -1, -1, 1, 2, 3}, M = 7Output: 5 6 0 1 2 3 M = 7, so value at index 2 should be (5+1) % 7 = 6 value at index 3 should be (6+1) % 7 = 0Input: arr[] = {5, -1, 7, -1, 9, 0}, M = 10Output: 5 6 7 8 9 0

**Approach:** First find the index of the non negative value index i. Then simply go in two directions i.e. From i-1 to 0 and i+1 to n.

- For index i-1 the value can be calculated by (a[i+1]-1+m)%m because (a – b) mod p = ((a mod p – b mod p) + p) mod p.
- For indexes i+1 the values can be calculated by (a[i-1]+1)%m.

Below is the implementation of the above approach:

`# Python implementation of the above approach ` `def` `construct(n, m, a): ` ` ` `ind ` `=` `0` ` ` ` ` `# Finding the index which is not -1 ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(a[i]!` `=` `-` `1` `): ` ` ` `ind ` `=` `i ` ` ` `break` ` ` ` ` `# Calculating the values of the indexes ind-1 to 0 ` ` ` `for` `i ` `in` `range` `(ind` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `if` `(a[i]` `=` `=` `-` `1` `): ` ` ` `a[i]` `=` `(a[i ` `+` `1` `]` `-` `1` `+` `m)` `%` `m ` ` ` ` ` `# Calculating the values of the indexes ind + 1 to n ` ` ` `for` `i ` `in` `range` `(ind ` `+` `1` `, n): ` ` ` `if` `(a[i]` `=` `=` `-` `1` `): ` ` ` `a[i]` `=` `(a[i` `-` `1` `]` `+` `1` `)` `%` `m ` ` ` `print` `(` `*` `a) ` ` ` `# Driver code ` `n, m ` `=` `6` `, ` `7` `a ` `=` `[` `5` `, ` `-` `1` `, ` `-` `1` `, ` `1` `, ` `2` `, ` `3` `] ` `construct(n, m, a) ` |

*chevron_right*

*filter_none*

**Output:**

5 6 0 1 2 3

## Recommended Posts:

- Game of replacing array elements
- Replacing an element makes array elements consecutive
- Maximize the value of A by replacing some of its digits with digits of B
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Generate original array from an array that store the counts of greater elements on right
- Print modified array after multiple array range increment operations
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Find minimum value to assign all array elements so that array product becomes greater
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Maximum subarray sum in array formed by repeating the given array k times
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Kth smallest element in the array using constant space when array can't be modified

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.