Related Articles

# Modify array by replacing every array element with minimum possible value of arr[j] + |j – i|

• Last Updated : 14 Jun, 2021

Given an array arr[] of size N, the task is to find a value for each index such that the value at index i is arr[j] + |j – i| where 1 ≤ j ≤ N, the task is to find the minimum value for each index from 1 to N.

Example:

Input: N = 5, arr[] = {1, 4, 2, 5, 3}
Output: {1, 2, 2, 3, 3}
Explanation:
arr[0] = arr[0] + |0-0| = 1
arr[1] = arr[0] + |0-1| = 2
arr[2] = arr[2] + |2-2| = 2
arr[3] = arr[2] + |2-3| = 3
arr[4] = arr[4] + |4-4| = 3
The output array will give minimum value at every ith position.

Input: N = 4, arr[] = {1, 2, 3, 4}
Output: {1, 2, 3, 4}

Naive Approach: The idea is to use two nested for loops for traversing the array and for each ith index, find and print the minimum value of arr[j] + |i-j|

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The idea is to use prefix sum technique from both left and right array traversal and find the minimum for each index. Follow the steps below to solve the problem:

1. Take two auxiliary array dp1[] and dp2[] where dp1[] store the answer for the left to right traversal and dp2[] stores the answer for the right to left traversal.
2. Traverse the array arr[] from i = 2 to N-1 and calculate min(arr[i], dp1[i-1] + 1).
3. Traverse the array arr[] form i = N-1 to 1 and calculate min(arr[i], dp2[i+1] + 1).
4. Again traverse the array from 1 to N and print min(dp1[i], dp2[i]) at each iteration.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find minimum value of``// arr[j] + |j - i| for every array index``void` `minAtEachIndex(``int` `n, ``int` `arr[])``{``    ``// Stores minimum of a[j] + |i - j|``    ``// upto position i``    ``int` `dp1[n];` `    ``// Stores minimum of a[j] + |i-j|``    ``// upto position i from the end``    ``int` `dp2[n];` `    ``int` `i;` `    ``dp1[0] = arr[0];` `    ``// Traversing and storing minimum``    ``// of a[j]+|i-j| upto i``    ``for` `(i = 1; i < n; i++)``        ``dp1[i] = min(arr[i], dp1[i - 1] + 1);` `    ``dp2[n - 1] = arr[n - 1];` `    ``// Traversing and storing minimum``    ``// of a[j]+|i-j| upto i from the end``    ``for` `(i = n - 2; i >= 0; i--)``        ``dp2[i] = min(arr[i], dp2[i + 1] + 1);` `    ``vector<``int``> v;` `    ``// Traversing from [0, N] and storing minimum``    ``// of a[j] + |i - j| from starting and end``    ``for` `(i = 0; i < n; i++)``        ``v.push_back(min(dp1[i], dp2[i]));` `    ``// Print the required array``    ``for` `(``auto` `x : v)``        ``cout << x << ``" "``;``}` `// Driver code``int` `main()``{` `    ``// Given array arr[]``    ``int` `arr[] = { 1, 4, 2, 5, 3 };` `    ``// Size of the array``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Function Call``    ``minAtEachIndex(N, arr);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``import` `java.util.ArrayList;``import` `java.util.List;``  ` `class` `GFG{``      ` `// Function to find minimum value of``// arr[j] + |j - i| for every array index``static` `void` `minAtEachIndex(``int` `n, ``int` `arr[])``{``    ` `    ``// Stores minimum of a[j] + |i - j|``    ``// upto position i``    ``int` `dp1[] = ``new` `int``[n];``    ` `    ``// Stores minimum of a[j] + |i-j|``    ``// upto position i from the end``    ``int` `dp2[] = ``new` `int``[n];` `    ``int` `i;` `    ``dp1[``0``] = arr[``0``];` `    ``// Traversing and storing minimum``    ``// of a[j]+|i-j| upto i``    ``for``(i = ``1``; i < n; i++)``        ``dp1[i] = Math.min(arr[i], dp1[i - ``1``] + ``1``);` `    ``dp2[n - ``1``] = arr[n - ``1``];` `    ``// Traversing and storing minimum``    ``// of a[j]+|i-j| upto i from the end``    ``for``(i = n - ``2``; i >= ``0``; i--)``        ``dp2[i] = Math.min(arr[i], dp2[i + ``1``] + ``1``);` `    ``ArrayList v = ``new` `ArrayList();` `    ``// Traversing from [0, N] and storing minimum``    ``// of a[j] + |i - j| from starting and end``    ``for``(i = ``0``; i < n; i++)``        ``v.add(Math.min(dp1[i], dp2[i]));` `    ``// Print the required array``    ``for``(``int` `x : v)``        ``System.out.print(x + ``" "``);``}``  ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given array arr[]``    ``int` `arr[] = { ``1``, ``4``, ``2``, ``5``, ``3` `};` `    ``// Size of the array``    ``int` `N = arr.length;` `    ``// Function Call``    ``minAtEachIndex(N, arr);``}``}` `// This code is contributed by sanjoy_62`

## Python3

 `# Python3 program for the above approach` `# Function to find minimum value of``# arr[j] + |j - i| for every array index``def` `minAtEachIndex(n, arr):``    ` `    ``# Stores minimum of a[j] + |i - j|``    ``# upto position i``    ``dp1 ``=` `[``0``] ``*` `n``    ` `    ``# Stores minimum of a[j] + |i-j|``    ``# upto position i from the end``    ``dp2 ``=` `[``0``] ``*` `n``    ` `    ``i ``=` `0` `    ``dp1[``0``] ``=` `arr[``0``]``    ` `    ``# Traversing and storing minimum``    ``# of a[j]+|i-j| upto i``    ``for` `i ``in` `range``(``1``, n):``        ``dp1[i] ``=` `min``(arr[i], dp1[i ``-` `1``] ``+` `1``)` `    ``dp2[n ``-` `1``] ``=` `arr[n ``-` `1``]` `    ``# Traversing and storing minimum``    ``# of a[j]+|i-j| upto i from the end``    ``for` `i ``in` `range``(n ``-` `2``, ``-``1``, ``-``1``):``        ``dp2[i] ``=` `min``(arr[i], dp2[i ``+` `1``] ``+` `1``)` `    ``v ``=` `[]` `    ``# Traversing from [0, N] and storing minimum``    ``# of a[j] + |i - j| from starting and end``    ``for` `i ``in` `range``(``0``, n):``        ``v.append(``min``(dp1[i], dp2[i]))` `    ``# Print the required array``    ``for` `x ``in` `v:``        ``print``(x, end ``=` `" "``)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given array arr``    ``arr ``=` `[ ``1``, ``4``, ``2``, ``5``, ``3` `]` `    ``# Size of the array``    ``N ``=` `len``(arr)` `    ``# Function Call``    ``minAtEachIndex(N, arr)` `# This code is contributed by shikhasingrajput`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{``      ` `// Function to find minimum value of``// arr[j] + |j - i| for every array index``static` `void` `minAtEachIndex(``int` `n, ``int` `[]arr)``{``    ` `    ``// Stores minimum of a[j] + |i - j|``    ``// upto position i``    ``int` `[]dp1 = ``new` `int``[n];``    ` `    ``// Stores minimum of a[j] + |i-j|``    ``// upto position i from the end``    ``int` `[]dp2 = ``new` `int``[n];``    ``int` `i;``    ``dp1[0] = arr[0];` `    ``// Traversing and storing minimum``    ``// of a[j]+|i-j| upto i``    ``for``(i = 1; i < n; i++)``        ``dp1[i] = Math.Min(arr[i], dp1[i - 1] + 1);``    ``dp2[n - 1] = arr[n - 1];` `    ``// Traversing and storing minimum``    ``// of a[j]+|i-j| upto i from the end``    ``for``(i = n - 2; i >= 0; i--)``        ``dp2[i] = Math.Min(arr[i], dp2[i + 1] + 1);``    ``List<``int``> v = ``new` `List<``int``>();` `    ``// Traversing from [0, N] and storing minimum``    ``// of a[j] + |i - j| from starting and end``    ``for``(i = 0; i < n; i++)``        ``v.Add(Math.Min(dp1[i], dp2[i]));` `    ``// Print the required array``    ``foreach``(``int` `x ``in` `v)``        ``Console.Write(x + ``" "``);``}``  ` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given array []arr``    ``int` `[]arr = { 1, 4, 2, 5, 3 };` `    ``// Size of the array``    ``int` `N = arr.Length;` `    ``// Function Call``    ``minAtEachIndex(N, arr);``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``
Output:
`1 2 2 3 3`

Time Complexity: O(N)
Auxiliary 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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up