给定一个具有n个元素的数组arr [] ,任务是找到所需的最小操作数,以便以共同的差为1的方式实现数组元素的算术级数。在单个操作中,任何元素都可以加1 。
例子:
Input: arr[] = {4, 4, 5, 5, 7}
Output: 5
Desired array is {4, 5, 6, 7, 8} which
can be achieved in minimum possible operations.
Input: arr[] = {11, 2, 5, 6}
Output: 26
Since we are allowed to do only increment, we
change the array to {11, 12, 13, 14}
方法:
- 我们可以利用二进制搜索来解决这个问题。
- 我们将使用固定的last元素构建所需的数组,如果解决方案无效,则将增加该值;如果有效,则减少该值,就像在二进制搜索中一样。
- 检查所需数组的所有元素都大于或等于输入数组,以便对元素执行操作以使其等于所需元素。查找操作数。
- 找到满足所需数组中所有元素条件的最后一个元素的最小可能值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that return true if the
// required array can be generated
// with m as the last element
bool check(int m, int n, int arr[])
{
// Build the desired array
int desired[n];
for (int i = n - 1; i >= 0; i--) {
desired[i] = m;
m--;
}
// Check if the given array can
// be converted to the desired array
// with the given operation
for (int i = 0; i < n; i++) {
if (arr[i] > desired[i] || desired[i] < 1) {
return false;
}
}
return true;
}
// Function to return the minimum number
// of operations required to convert the
// given array to an increasing AP series
// with common difference as 1
int minOperations(int arr[], int n)
{
int start = (int)arr[n - 1];
int end = *(max_element(arr, arr + n)) + n;
int max_arr = 0;
// Apply Binary Search
while (start <= end) {
int mid = (start + end) / 2;
// If array can be generated with
// mid as the last element
if (check(mid, n, arr)) {
// Current ans is mid
max_arr = mid;
// Check whether the same can be
// achieved with even less operations
end = mid - 1;
}
else {
start = mid + 1;
}
}
// Build the desired array
int desired[n];
for (int i = n - 1; i >= 0; i--) {
desired[i] = max_arr;
max_arr--;
}
// Calculate the number of
// operations required
int operations = 0;
for (int i = 0; i < n; i++) {
operations += (desired[i] - arr[i]);
}
// Return the number of
// operations required
return operations;
}
// Driver code
int main()
{
int arr[] = { 4, 4, 5, 5, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minOperations(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
class GFG
{
// Function that return true if the
// required array can be generated
// with m as the last element
static boolean check(int m, int n, int arr[])
{
// Build the desired array
int[] desired = new int[n];
for (int i = n - 1; i >= 0; i--)
{
desired[i] = m;
m--;
}
// Check if the given array can
// be converted to the desired array
// with the given operation
for (int i = 0; i < n; i++)
{
if (arr[i] > desired[i] || desired[i] < 1)
{
return false;
}
}
return true;
}
// Function to return the minimum number
// of operations required to convert the
// given array to an increasing AP series
// with common difference as 1
static int minOperations(int arr[], int n)
{
int start = (int) arr[n - 1];
int end = Arrays.stream(arr).max().getAsInt() + n;
int max_arr = 0;
// Apply Binary Search
while (start <= end)
{
int mid = (start + end) / 2;
// If array can be generated with
// mid as the last element
if (check(mid, n, arr))
{
// Current ans is mid
max_arr = mid;
// Check whether the same can be
// achieved with even less operations
end = mid - 1;
}
else
{
start = mid + 1;
}
}
// Build the desired array
int[] desired = new int[n];
for (int i = n - 1; i >= 0; i--)
{
desired[i] = max_arr;
max_arr--;
}
// Calculate the number of
// operations required
int operations = 0;
for (int i = 0; i < n; i++)
{
operations += (desired[i] - arr[i]);
}
// Return the number of
// operations required
return operations;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {4, 4, 5, 5, 7};
int n = arr.length;
System.out.println(minOperations(arr, n));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation of the approach
# Function that return true if the
# required array can be generated
# with m as the last element
def check( m, n, arr) :
# Build the desired array
desired = [0]*n;
for i in range(n-1,-1,-1) :
desired[i] = m;
m -= 1;
# Check if the given array can
# be converted to the desired array
# with the given operation
for i in range(n) :
if (arr[i] > desired[i] or desired[i] < 1) :
return False;
return True
# Function to return the minimum number
# of operations required to convert the
# given array to an increasing AP series
# with common difference as 1
def minOperations(arr, n) :
start = arr[n - 1];
end = max(arr) + n;
max_arr = 0;
# Apply Binary Search
while (start <= end) :
mid = (start + end) // 2;
# If array can be generated with
# mid as the last element
if (check(mid, n, arr)) :
# Current ans is mid
max_arr = mid;
# Check whether the same can be
# achieved with even less operations
end = mid - 1;
else :
start = mid + 1;
# Build the desired array
desired = [0]* n;
for i in range(n-1, -1,-1) :
desired[i] = max_arr;
max_arr -= 1;
# Calculate the number of
# operations required
operations = 0;
for i in range(n) :
operations += (desired[i] - arr[i]);
# Return the number of
# operations required
return operations;
# Driver code
if __name__ == "__main__" :
arr = [ 4, 4, 5, 5, 7 ];
n = len(arr);
print(minOperations(arr, n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Linq;
class GFG
{
// Function that return true if the
// required array can be generated
// with m as the last element
static Boolean check(int m, int n, int []arr)
{
// Build the desired array
int[] desired = new int[n];
for (int i = n - 1; i >= 0; i--)
{
desired[i] = m;
m--;
}
// Check if the given array can
// be converted to the desired array
// with the given operation
for (int i = 0; i < n; i++)
{
if (arr[i] > desired[i] || desired[i] < 1)
{
return false;
}
}
return true;
}
// Function to return the minimum number
// of operations required to convert the
// given array to an increasing AP series
// with common difference as 1
static int minOperations(int []arr, int n)
{
int start = (int) arr[n - 1];
int end = arr.Max() + n;
int max_arr = 0;
// Apply Binary Search
while (start <= end)
{
int mid = (start + end) / 2;
// If array can be generated with
// mid as the last element
if (check(mid, n, arr))
{
// Current ans is mid
max_arr = mid;
// Check whether the same can be
// achieved with even less operations
end = mid - 1;
}
else
{
start = mid + 1;
}
}
// Build the desired array
int[] desired = new int[n];
for (int i = n - 1; i >= 0; i--)
{
desired[i] = max_arr;
max_arr--;
}
// Calculate the number of
// operations required
int operations = 0;
for (int i = 0; i < n; i++)
{
operations += (desired[i] - arr[i]);
}
// Return the number of
// operations required
return operations;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {4, 4, 5, 5, 7};
int n = arr.Length;
Console.WriteLine(minOperations(arr, n));
}
}
// This code contributed by Rajput-Ji
输出:
5