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 = 7
Output: 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 = 0
Input: arr[] = {5, -1, 7, -1, 9, 0}, M = 10
Output: 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:
C++
// C++ implemenatation of above approach #include <bits/stdc++.h> using namespace std; void construct(int n, int m, int a[]) { int ind = 0; // Finding the index which is not -1 for (int i = 0; i < n; i++) { if (a[i] != -1) { ind = i; break; } } // Calculating the values of // the indexes ind-1 to 0 for (int i = ind - 1; i > -1; i--) { if (a[i] == -1) a[i] = (a[i + 1] - 1 + m) % m; } // Calculating the values of // the indexes ind + 1 to n for (int i = ind + 1; i < n; i++) { if (a[i] == -1) a[i] = (a[i - 1] + 1) % m; } for (int i = 0; i < n; i++) { cout<< a[i] << " "; } } // Driver code int main() { int n = 6, m = 7; int a[] = { 5, -1, -1, 1, 2, 3 }; construct(n, m, a); return 0; } // This code is contributed by 29AjayKumar |
Java
// Java implementation of the above approach class GFG { static void construct(int n, int m, int[] a) { int ind = 0; // Finding the index which is not -1 for (int i = 0; i < n; i++) { if (a[i] != -1) { ind = i; break; } } // Calculating the values of // the indexes ind-1 to 0 for (int i = ind - 1; i > -1; i--) { if (a[i] == -1) a[i] = (a[i + 1] - 1 + m) % m; } // Calculating the values of // the indexes ind + 1 to n for (int i = ind + 1; i < n; i++) { if (a[i] == -1) a[i] = (a[i - 1] + 1) % m; } for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } } // Driver code public static void main(String[] args) { int n = 6, m = 7; int[] a = { 5, -1, -1, 1, 2, 3 }; construct(n, m, a); } } // This code is contributed by 29AjayKumar |
Python3
# 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, 7a =[5, -1, -1, 1, 2, 3] construct(n, m, a) |
C#
// C# implementation of the above approach using System; class GFG { static void construct(int n, int m, int[] a) { int ind = 0; // Finding the index which is not -1 for (int i = 0; i < n; i++) { if (a[i] != -1) { ind = i; break; } } // Calculating the values of // the indexes ind-1 to 0 for (int i = ind - 1; i > -1; i--) { if (a[i] == -1) a[i] = (a[i + 1] - 1 + m) % m; } // Calculating the values of // the indexes ind + 1 to n for (int i = ind + 1; i < n; i++) { if (a[i] == -1) a[i] = (a[i - 1] + 1) % m; } for (int i = 0; i < n; i++) { Console.Write(a[i] + " "); } } // Driver code public static void Main(String[] args) { int n = 6, m = 7; int[] a = { 5, -1, -1, 1, 2, 3 }; construct(n, m, a); } } // This code is contributed by 29AjayKumar |
5 6 0 1 2 3
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 array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Game of replacing array elements
- Replacing an element makes array elements consecutive
- Maximum possible GCD after replacing at most one element in the given array
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Most frequent element in Array after replacing given index by K for Q queries
- Array sum after replacing all occurrences of X by Y for Q queries
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Queries to find the XOR of an Array after replacing all occurrences of X by Y
- Maximum Subarray Sum possible by replacing an Array element by its Square
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Minimize Array length by repeatedly replacing co-prime pairs with 1
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Maximize the value of A by replacing some of its digits with digits of B
- Count of strings possible by replacing two consecutive same character with new character
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- 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
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.
Improved By : 29AjayKumar

