Minimize sum of absolute values of A[i] – (B + i) for a given array
Last Updated :
24 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int MinSum( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
arr[i] = arr[i] - (i + 1);
}
sort(arr, arr + N);
int b = arr[N / 2];
int ans = 0;
for ( int i = 0; i < N; i++) {
ans += abs (arr[i] - b);
}
return ans;
}
int main()
{
int arr[] = { 2, 2, 3, 5, 5 };
int N = sizeof (arr) / sizeof ( int );
int ans = MinSum(arr, N);
cout << ans << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MinSum( int arr[], int N)
{
for ( int i = 0 ; i < N; i++) {
arr[i] = arr[i] - (i + 1 );
}
Arrays.sort(arr);
int b = arr[N / 2 ];
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
ans += Math.abs(arr[i] - b);
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 3 , 5 , 5 };
int N = arr.length;
int ans = MinSum(arr, N);
System.out.print(ans);
}
}
|
Python3
def MinSum(arr, N):
for i in range (N):
arr[i] - = (i + 1 )
arr.sort()
b = arr[N / / 2 ]
ans = 0
for i in range (N):
ans + = abs (arr[i] - b)
return ans
arr = [ 2 , 2 , 3 , 5 , 5 ]
N = len (arr)
print (MinSum(arr, N))
|
C#
using System;
class GFG{
static int MinSum( int []arr, int N)
{
for ( int i = 0; i < N; i++)
{
arr[i] = arr[i] - (i + 1);
}
Array.Sort(arr);
int b = arr[N / 2];
int ans = 0;
for ( int i = 0; i < N; i++)
{
ans += Math.Abs(arr[i] - b);
}
return ans;
}
static void Main()
{
int []arr = { 2, 2, 3, 5, 5 };
int N = arr.Length;
int ans = MinSum(arr, N);
Console.Write(ans);
}
}
|
Javascript
<script>
function MinSum(arr, N) {
for (let i = 0; i < N; i++) {
arr[i] = arr[i] - (i + 1);
}
arr.sort( function (a, b) { return a - b });
let b = arr[(Math.floor(N / 2))];
let ans = 0;
for (let i = 0; i < N; i++) {
ans += Math.abs(arr[i] - b);
}
return ans;
}
let arr = [2, 2, 3, 5, 5];
let N = arr.length;
let ans = MinSum(arr, N);
document.write(ans + "<br>" );
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...