最小化递增或递减操作以使给定的 Array 元素连续
给定一个数组, arr[] ,大小为N .,任务是对数组的元素进行最小递增或递减操作,使所有元素连续,输出所有可能变化的最小和(加法和减法)需要做同样的事情。
例子:
Input: N = 5, arr[] = {13, 6, 11, 18, 4}
Output: 15
Explanation: Convert 4 to 9, 8 to 10, 13 to12 and 18to13, the new array becomes {9, 10, 11, 12, 13}.
So the sum of changes are abs(9-4) + abs(10-8) + abs(12-13) + abs(13-18) = 15.
Input: N = 2, arr[] = {3, 8}
Output: 4
方法:可以使用观察来解决任务。数组元素的中位数应保持不变,其余元素应相应更改,以使所有元素变为连续。
请按照以下步骤解决问题:
- 对数组进行排序
- 取一个存储数组中位数的变量mid和一个存储其位置的变量pos 。
- 另外,取一个变量ele并用 这 结果数组的最小值 即(mid – pos)和一个变量sum = 0来存储数组元素中所有可能变化的总和。
- 遍历数组并在每次第 i次迭代中:
- 用abs(arr[i]-ele)递增总和(添加原始元素和所需元素的差)
- 将ele增加 1
- 输出总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to output sum of all the
// required changes in array
int minChanges(int arr[], int N)
{
sort(arr, arr + N);
// Stores median of array
int mid = arr[N / 2];
// Variable for position of median
int pos = N / 2;
// Smallest element of
// the required array
int ele = mid - pos;
int sum = 0;
// Loop to find sum of changes
for (int i = 0; i < N; i++) {
// Adding difference of original
// and required element to answer
sum += abs(arr[i] - ele);
ele++;
}
return sum;
}
// Driver code
int main()
{
int N = 5;
int arr[] = { 13, 6, 11, 18, 4 };
cout << minChanges(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
// Function to output sum of all the
// required changes in array
static int minChanges(int arr[], int N)
{
Arrays.sort(arr);
// Stores median of array
int mid = arr[N / 2];
// Variable for position of median
int pos = N / 2;
// Smallest element of
// the required array
int ele = mid - pos;
int sum = 0;
// Loop to find sum of changes
for (int i = 0; i < N; i++)
{
// Adding difference of original
// and required element to answer
sum += Math.abs(arr[i] - ele);
ele++;
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int N = 5;
int arr[] = { 13, 6, 11, 18, 4 };
int ans = minChanges(arr, N);
System.out.println(ans);
}
}
// This code is contributed by hrithikgarg03188
Python3
# Python code for the above approach
import math as Math
# Function to output sum of all the
# required changes in array
def minChanges(arr, N):
arr.sort();
# Stores median of array
mid = arr[N // 2];
# Variable for position of median
pos = N // 2;
# Smallest element of
# the required array
ele = mid - pos;
sum = 0;
# Loop to find sum of changes
for i in range(N):
# Adding difference of original
# and required element to answer
sum += Math.fabs(arr[i] - ele);
ele += 1
return int(sum);
# Driver code
N = 5;
arr = [13, 6, 11, 18, 4];
print(minChanges(arr, N));
# This code is contributed by Saurabh Jaiswal
C#
// C# program for above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to output sum of all the
// required changes in array
static int minChanges(int[ ] arr, int N)
{
Array.Sort(arr);
// Stores median of array
int mid = arr[N / 2];
// Variable for position of median
int pos = N / 2;
// Smallest element of
// the required array
int ele = mid - pos;
int sum = 0;
// Loop to find sum of changes
for (int i = 0; i < N; i++)
{
// Adding difference of original
// and required element to answer
sum += Math.Abs(arr[i] - ele);
ele++;
}
return sum;
}
// Driver code
public static void Main(String[] args)
{
int N = 5;
int[ ] arr = { 13, 6, 11, 18, 4 };
Console.WriteLine(minChanges(arr, N));
}
}
// This code is contributed by hrithikgarg03188
Javascript
输出
15
时间复杂度:O(N * logN)
辅助空间:O(1)