Open in App
Not now

# Minimize sum of absolute values of A[i] – (B + i) for a given array

• Last Updated : 20 Jul, 2021

Given an array arr[ ] of size N, the task is to find the minimum possible value of the expression abs(arr[1] – (b + 1))  + abs(arr[2] – (b + 2)) . . . abs(arr[N] – (b + N)), where b is an independent integer.

Input: arr[ ] = { 2, 2, 3, 5, 5 }
Output: 2
Explanation: Considering b = 0: The value of the expression is abs(2 – (0 + 1)) + abs(2 – (0 + 2)) + abs(3 – (0 + 3)) + abs(5 – (0 + 4)) + abs(5 – (0 + 5)) = 1 + 0 + 0 + 1 + 0 = 2
Therefore, the minimum possible value for the expression is 2.

Input: arr[ ] = { 6, 5, 4, 3, 2, 1 }
Output: 18

Approach: Considering B[i] = A[i] âˆ’ i, the problem is to reduces to minimize the sum of abs (B[i] âˆ’ b). It can be observed that it is best to have b as the median of the modified array B[]. So, after sorting array B[], the problem can be solved following the steps given below.

• Traverse the array arr[ ] and decrease every element by their index (i + 1).
• Sort the array in ascending order.
• Now, choose b as the median of arr[ ], say b = arr[N/2].
• Initialize a variable, say ans as 0, to store the minimum possible value of the expression.
• Traverse the array again and update ans as abs(arr[i] – b).
• Return the value of ans.

Below is the implementation of the above approach.

## C++

 `// C++ program for above approach` `#include ` `using` `namespace` `std;`   `// Function to calculate minimum` `// possible sum of all (arr[i] - b + i)` `int` `MinSum(``int` `arr[], ``int` `N)` `{`   `    ``// Modify the array` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``arr[i] = arr[i] - (i + 1);` `    ``}`   `    ``// Sort the array` `    ``sort(arr, arr + N);`   `    ``// Calculate median` `    ``int` `b = arr[N / 2];`   `    ``// Stores the required answer` `    ``int` `ans = 0;` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// Update answer` `        ``ans += ``abs``(arr[i] - b);` `    ``}`   `    ``// Return the answer` `    ``return` `ans;` `}`   `// Driver Code` `int` `main()` `{`   `    ``// Given Input` `    ``int` `arr[] = { 2, 2, 3, 5, 5 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(``int``);`   `    ``// Function Call` `    ``int` `ans = MinSum(arr, N);`   `    ``cout << ans << ``"\n"``;`   `    ``return` `0;` `}`

## Java

 `// Java program for above approach` `// Function to calculate minimum` `// possible sum of all (arr[i] - b + i)` `import` `java.util.*;` `class` `GFG{` `    `  `static` `int` `MinSum(``int` `arr[], ``int` `N)` `{`   `    ``// Modify the array` `    ``for` `(``int` `i = ``0``; i < N; i++) {` `        ``arr[i] = arr[i] - (i + ``1``);` `    ``}`   `    ``// Sort the array` `    ``Arrays.sort(arr);`   `    ``// Calculate median` `    ``int` `b = arr[N / ``2``];`   `    ``// Stores the required answer` `    ``int` `ans = ``0``;` `    ``for` `(``int` `i = ``0``; i < N; i++) {`   `        ``// Update answer` `        ``ans += Math.abs(arr[i] - b);` `    ``}`   `    ``// Return the answer` `    ``return` `ans;` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``// Given Input` `    ``int` `arr[] = { ``2``, ``2``, ``3``, ``5``, ``5` `};` `    ``int` `N = arr.length;`   `    ``// Function Call` `    ``int` `ans = MinSum(arr, N);`   `    ``System.out.print(ans);`   `}` `}`   `// This code is contributed by shivanisinghss2110`

## Python3

 `# Java program for above approach` `# Function to calculate minimum` `# possible sum of all (arr[i] - b + i)` `def` `MinSum(arr, N):` `  `  `  ``# Modify the array` `    ``for` `i ``in` `range``(N):` `        ``arr[i] ``-``=` `(i``+``1``)` `        `  `    ``# sort the array    ` `    ``arr.sort()` `    `  `    ``# calculate median` `    ``b ``=` `arr[N``/``/``2``]` `    `  `     ``# Stores the required answer` `    ``ans ``=` `0` `    ``for` `i ``in` `range``(N):` `      `  `      ``# Update answer` `        ``ans ``+``=` `abs``(arr[i]``-``b)` `        `  `        ``# Return the answer` `    ``return` `ans`   `# Driver code` `arr ``=` `[``2``, ``2``, ``3``, ``5``, ``5``]` `N ``=` `len``(arr)` `print``(MinSum(arr, N))`   `# This code is contributed by Parth Manchanda`

## C#

 `// C# program for above approach` `using` `System;`   `class` `GFG{`   `// Function to calculate minimum` `// possible sum of all (arr[i] - b + i)` `static` `int` `MinSum(``int` `[]arr, ``int` `N)` `{` `    `  `    ``// Modify the array` `    ``for``(``int` `i = 0; i < N; i++) ` `    ``{` `        ``arr[i] = arr[i] - (i + 1);` `    ``}`   `    ``// Sort the array` `    ``Array.Sort(arr);`   `    ``// Calculate median` `    ``int` `b = arr[N / 2];`   `    ``// Stores the required answer` `    ``int` `ans = 0;` `    ``for``(``int` `i = 0; i < N; i++) ` `    ``{` `        `  `        ``// Update answer` `        ``ans += Math.Abs(arr[i] - b);` `    ``}`   `    ``// Return the answer` `    ``return` `ans;` `}`   `// Driver Code` `static` `void` `Main() ` `{` `    `  `    ``// Given Input` `    ``int` `[]arr = { 2, 2, 3, 5, 5 };` `    ``int` `N = arr.Length;`   `    ``// Function Call` `    ``int` `ans = MinSum(arr, N);`   `    ``Console.Write(ans);` `}` `}`   `// This code is contributed by SoumikMondal`

## Javascript

 ``

Output:

`2`

Time Complexity: O(N*logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles