使 AP 中的所有数组元素所需的最小增量或减量 1
给定一个由N个整数组成的数组arr[] ,任务是找到对数组元素执行递增/递减 1 的最小数量,以使给定数组arr[]的所有元素在 AP 中。如果无法在 AP 中创建数组,则打印“-1” 。
例子:
Input: arr[] = {19, 16, 9, 5, 0}
Output: 3
Explanation:
Following are the order of increment/decrements of array elements is made:
- Increment array element arr[0](= 19) by 1.
- Decrement array element arr[1](= 16) by 1.
- Increment array element arr[2](= 9) by 1.
After the above operations, the array arr[] modifies to {20, 15, 10, 5, 0}, which is in AP with first term 20 and common difference -5. Therefore, the total count of element is 3.
Input: arr[] = {1, 2, 3, 4, 10}
Output: -1
方法:给定的问题可以通过找到第一项和前两个元素的共同差来解决,然后通过简单地迭代数组来检查是否所有元素都可以更改为具有给定的第一项和共同差的 AP 序列。请按照以下步骤解决问题:
- 如果N小于等于2 ,则 print 0 ,因为每个这样的序列都是算术级数。
- 初始化一个变量,比如res为N + 1来存储答案。
- 使用变量a在[-1, 1]范围内迭代并执行以下步骤:
- 使用变量b在[-1, 1]范围内迭代并执行以下步骤:
- 初始化一个变量,将更改为0以存储数组arr[] 的更改元素的计数。
- 如果a不等于0 ,则将更改的值增加1 。
- 如果b不等于0 ,则将更改的值增加1 。
- 初始化一个变量,比如说, orig为arr[0] + a来存储第一个元素,而diff为(arr[1] + b) – (arr[0] + a)来存储等差数列的公差。
- 初始化一个变量,比如true以存储具有第一项orig和公差diff的算术级数序列是否可能。
- 使用变量i在[2, N-1]范围内迭代并执行以下步骤:
- 将变量actual初始化为orig+i*diff以将等差数列的实际元素存储在索引i处。
- 如果abs(actual – arr[i])大于1 ,那么这种算术级数是不可达的。然后将good的值设置为false并打破循环。
- 否则,如果实际不等于arr[i],则将更改的值增加1 。
- 遍历内部for循环后,将res的值更新为min(changes, res)。
- 使用变量b在[-1, 1]范围内迭代并执行以下步骤:
- 完成上述步骤后,如果 res 大于N则将 -1 分配给res 。否则,打印res的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of elements to be changed to convert
// the array into an AP
int findMinimumMoves(int N, int arr[])
{
// If N is less than 3
if (N <= 2) {
return 0;
}
// Stores the answer
int res = N + 1;
// Iterate in the range [-1, 1]
for (int a = -1; a <= 1; a++) {
// Iterate in the range [-1, 1]
for (int b = -1; b <= 1; b++) {
// Stores the total changes
int changes = 0;
if (a != 0) {
changes++;
}
if (b != 0) {
changes++;
}
// Stores the first element
// of the AP
int orig = (arr[0] + a);
// Stores the common difference
// of the AP
int diff = (arr[1] + b) - (arr[0] + a);
// Stores whether it is
// possible to convert the
// array into AP
bool good = true;
// Iterate in the range [2, N-1]
for (int i = 2; i < N; i++) {
// Stores the ith element
// of the AP
int actual = orig + i * diff;
// If abs(actual-arr[i])
// is greater than 1
if (abs(actual - arr[i]) > 1) {
// Mark as false
good = false;
break;
}
// If actual is not
// equal to arr[i]
if (actual != arr[i])
changes++;
}
if (!good)
continue;
// Update the value of res
res = min(res, changes);
}
}
// If res is greater than N
if (res > N)
res = -1;
// Return the value of res
return res;
}
// Driver Code
int main()
{
int arr[] = { 19, 16, 9, 5, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << findMinimumMoves(N, arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the minimum number
// of elements to be changed to convert
// the array into an AP
static int findMinimumMoves(int N, int arr[])
{
// If N is less than 3
if (N <= 2)
{
return 0;
}
// Stores the answer
int res = N + 1;
// Iterate in the range [-1, 1]
for(int a = -1; a <= 1; a++)
{
// Iterate in the range [-1, 1]
for(int b = -1; b <= 1; b++)
{
// Stores the total changes
int changes = 0;
if (a != 0)
{
changes++;
}
if (b != 0)
{
changes++;
}
// Stores the first element
// of the AP
int orig = (arr[0] + a);
// Stores the common difference
// of the AP
int diff = (arr[1] + b) - (arr[0] + a);
// Stores whether it is
// possible to convert the
// array into AP
boolean good = true;
// Iterate in the range [2, N-1]
for(int i = 2; i < N; i++)
{
// Stores the ith element
// of the AP
int actual = orig + i * diff;
// If abs(actual-arr[i])
// is greater than 1
if (Math.abs(actual - arr[i]) > 1)
{
// Mark as false
good = false;
break;
}
// If actual is not
// equal to arr[i]
if (actual != arr[i])
changes++;
}
if (!good)
continue;
// Update the value of res
res = Math.min(res, changes);
}
}
// If res is greater than N
if (res > N)
res = -1;
// Return the value of res
return res;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 19, 16, 9, 5, 0 };
int N = arr.length;
System.out.println(findMinimumMoves(N, arr));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program for the above approach
# Function to find the minimum number
# of elements to be changed to convert
# the array into an AP
def findMinimumMoves(N, arr):
# If N is less than 3
if (N <= 2):
return 0
# Stores the answer
res = N + 1
# Iterate in the range [-1, 1]
for a in range(-1, 2, 1):
# Iterate in the range [-1, 1]
for b in range(-1, 2, 1):
# Stores the total changes
changes = 0
if (a != 0):
changes += 1
if (b != 0):
changes += 1
# Stores the first element
# of the AP
orig = (arr[0] + a)
# Stores the common difference
# of the AP
diff = (arr[1] + b) - (arr[0] + a)
# Stores whether it is
# possible to convert the
# array into AP
good = True
# Iterate in the range [2, N-1]
for i in range(2, N, 1):
# Stores the ith element
# of the AP
actual = orig + i * diff
# If abs(actual-arr[i])
# is greater than 1
if (abs(actual - arr[i]) > 1):
# Mark as false
good = False
break
# If actual is not
# equal to arr[i]
if (actual != arr[i]):
changes += 1
if (good == False):
continue
# Update the value of res
res = min(res, changes)
# If res is greater than N
if (res > N):
res = -1
# Return the value of res
return res
# Driver Code
if __name__ == '__main__':
arr = [ 19, 16, 9, 5, 0 ]
N = len(arr)
print(findMinimumMoves(N, arr))
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum number
// of elements to be changed to convert
// the array into an AP
static int findMinimumMoves(int N, int[] arr)
{
// If N is less than 3
if (N <= 2)
{
return 0;
}
// Stores the answer
int res = N + 1;
// Iterate in the range [-1, 1]
for(int a = -1; a <= 1; a++)
{
// Iterate in the range [-1, 1]
for(int b = -1; b <= 1; b++)
{
// Stores the total changes
int changes = 0;
if (a != 0)
{
changes++;
}
if (b != 0)
{
changes++;
}
// Stores the first element
// of the AP
int orig = (arr[0] + a);
// Stores the common difference
// of the AP
int diff = (arr[1] + b) - (arr[0] + a);
// Stores whether it is
// possible to convert the
// array into AP
bool good = true;
// Iterate in the range [2, N-1]
for(int i = 2; i < N; i++)
{
// Stores the ith element
// of the AP
int actual = orig + i * diff;
// If abs(actual-arr[i])
// is greater than 1
if (Math.Abs(actual - arr[i]) > 1)
{
// Mark as false
good = false;
break;
}
// If actual is not
// equal to arr[i]
if (actual != arr[i])
changes++;
}
if (!good)
continue;
// Update the value of res
res = Math.Min(res, changes);
}
}
// If res is greater than N
if (res > N)
res = -1;
// Return the value of res
return res;
}
// Driver code
static public void Main()
{
int[] arr = { 19, 16, 9, 5, 0 };
int N = arr.Length;
Console.WriteLine(findMinimumMoves(N, arr));
}
}
// This code is contributed by target_2.
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)