生成给定 Array AP 所需的实数最少替换
给定一个包含N个整数的数组arr[] 。任务是将数组转换为具有尽可能少的替换次数的算术级数。在一次替换中,任何一个元素都可以被任何实数替换。
例子:
Input: N = 6, arr[] = { 3, -2, 4, -1, -4, 0 }
Output: 3
Explanation: Change arr[0] = -2.5, arr[2] = -1.5, arr[4] = -0.5
So, the new sequence is AP { -2.5, -2, -1.5, -1, -0.5, 0} with 0.5 as the common difference.
Input: N = 5, arr[] = { 1, 0, 2, 4, 5}
Output: 2
Explanation: Change arr[1] = 2, arr[2] = 3
So, the new sequence is { 1, 2, 3, 4, 5 } which is an AP.
方法:问题的解决方案是基于从数组中找到所有可能的共同差异。请按照以下步骤操作:
- 运行嵌套循环以从仅形成两个元素和 AP 的数组中查找所有可能的共同差异,并将它们存储在map中。
- 现在对于每个共同差异,遍历数组并找出存在于 AP中具有该特定差异的值的总数。
- 需要更改剩余数量的值。
- 这些剩余值中的最小值是所需答案。
下面是上述方法的实现:
C++
// C++ program to find the minimum number
// of changes required to make the given
// array an AP
#include
using namespace std;
// Function to find the minimum number
// of changes required to make the given
// array an AP
int minChanges(int arr[], int N)
{
if (N <= 2) {
return 0;
}
int ans = INT_MAX;
for (int i = 0; i < N; i++) {
// Map to store number of points
// that lie on the Ap
// with key as the common difference
unordered_map points;
for (int j = 0; j < N; j++) {
if (i == j)
continue;
// Calculating the common difference
// for the AP with arr[i] and arr[j]
double slope
= (double)(arr[j] - arr[i]) / (j - i);
points[slope]++;
}
int max_points = INT_MIN;
// Finding maximum number of values
// that lie on the Ap
for (auto point : points) {
max_points = max(max_points,
point.second);
}
max_points++;
ans = min(ans, N - max_points);
}
return ans;
}
// Driver code
int main()
{
int N = 6;
int arr[] = { 3, -2, 4, -1, -4, 0 };
// Function call
cout << minChanges(arr, N);
return 0;
}
Java
// JAVA program to find the minimum number
// of changes required to make the given
// array an AP
import java.util.*;
class GFG
{
// Function to find the minimum number
// of changes required to make the given
// array an AP
public static int minChanges(int[] arr, int N)
{
if (N <= 2) {
return 0;
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
// Map to store number of points
// that lie on the Ap
// with key as the common difference
HashMap points
= new HashMap<>();
for (int j = 0; j < N; j++) {
if (i == j)
continue;
// Calculating the common difference
// for the AP with arr[i] and arr[j]
double slope
= (double)(arr[j] - arr[i]) / (j - i);
if (points.containsKey(slope)) {
points.put(slope,
points.get(slope) + 1);
}
else {
points.put(slope, 1);
}
}
int max_points = Integer.MIN_VALUE;
// Finding maximum number of values
// that lie on the Ap
for (Map.Entry mp :
points.entrySet()) {
max_points
= Math.max(max_points, mp.getValue());
}
max_points++;
ans = Math.min(ans, N - max_points);
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int N = 6;
int[] arr = new int[] { 3, -2, 4, -1, -4, 0 };
// Function call
System.out.print(minChanges(arr, N));
}
}
// This code is contributed by Taranpreet
Python3
# Python3 program to find the minimum number
# of changes required to make the given array an AP.
def minChanges(arr, n):
if n <= 2:
return 0
ans = float('inf')
for i in range(n):
# Map to store number of points
# that lie on the Ap
# with key as the common difference
points = {}
for j in range(n):
if i == j:
continue
# Calculating the common difference
# for the AP with arr[i] and arr[j] double slope
slope = (arr[j] - arr[i])//(j - i)
if slope in points:
points[slope] += 1
else:
points[slope] = 1
max_points = float('-inf')
# Finding maximum number of values
# that lie on the Ap
for point in points:
max_points = max(max_points, points[point])
max_points += 1
ans = min(ans, n-max_points)
return ans
# Driver code
n = 6
arr = [3, -2, 4, -1, -4, 0]
print(minChanges(arr, n))
'''This code is written by Rajat Kumar (GLA University)'''
C#
// C# program to find the minimum number
// of changes required to make the given
// array an AP
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the minimum number
// of changes required to make the given
// array an AP
static int minChanges(int[] arr, int N)
{
if (N <= 2) {
return 0;
}
int ans = Int32.MaxValue;
for (int i = 0; i < N; i++) {
// Map to store number of points
// that lie on the Ap
// with key as the common difference
Dictionary points
= new Dictionary();
for (int j = 0; j < N; j++) {
if (i == j)
continue;
// Calculating the common difference
// for the AP with arr[i] and arr[j]
double slope
= (double)(arr[j] - arr[i]) / (j - i);
if (!points.ContainsKey(slope)) {
points.Add(slope, 1);
}
else {
points[slope] = points[slope] + 1;
}
}
int max_points = Int32.MinValue;
// Finding maximum number of values
// that lie on the Ap
foreach(
KeyValuePair point in points)
{
max_points
= Math.Max(max_points, point.Value);
}
max_points++;
ans = Math.Min(ans, N - max_points);
}
return ans;
}
// Driver code
public static void Main()
{
int N = 6;
int[] arr = { 3, -2, 4, -1, -4, 0 };
// Function call
Console.Write(minChanges(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
3
时间复杂度: O(N 2 )
辅助空间: O(N)