📜  最小化给定数组的 A[i] – (B + i) 的绝对值之和

📅  最后修改于: 2022-05-13 01:56:09.353000             🧑  作者: Mango

最小化给定数组的 A[i] – (B + i) 的绝对值之和

给定一个大小为 N数组arr[ ] ,任务是找到表达式abs(arr[1] – (b + 1)) + abs(arr[2] – (b + 2))的最小可能值. . abs(arr[N] – (b + N)) ,其中b是一个独立的整数。

方法:考虑B[i] = A[i] - i,问题是减少以最小化 abs (B[i] - b) 的总和。可以观察到,最好将b作为修改后的数组B[] 的中位数。因此,在对数组B[] 进行排序后,可以按照以下步骤解决问题

  • 遍历数组arr[ ]并按索引 (i + 1)减少每个元素
  • 按升序对数组进行排序。
  • 现在,选择b作为arr[ ]的中位数,例如b = arr[N/2]
  • 初始化一个变量,比如ans0,以存储表达式的最小可能值。
  • 再次遍历数组并将ans更新为abs(arr[i] – b)。
  • 返回ans的值。

下面是上述方法的实现。

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


输出:
2

时间复杂度: O(N*logN)
辅助空间: O(1)