📌  相关文章
📜  更新数组以满足给定条件所需的操作计数

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

更新数组以满足给定条件所需的操作计数

给定一个大小为N的数组arr[]和一个整数K 。任务是找到更新数组所需的操作,以便当任何索引j可以从索引i访问时,如果索引j与索引iabs(arr[ i] – arr[j]) ≤ K。在单个操作中,数组的任何元素都可以递增或递减 1。
例子:

方法:

  • 从第二个元素开始遍历数组,计算当前元素和前一个元素的绝对差。
  • 如果绝对差值大于K ,则需要更新当前元素,即将值添加到较小元素或从较大元素中减去值,以使绝对差值变为K

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// operations required to update
// the array such that it is possible
// to move from index 0 to index n - 1
int countOp(int arr[], int n, int k)
{
 
    int operations = 0;
    for (int i = 1; i < n; i++) {
 
        // Current element needs to be updated
        if (abs(arr[i] - arr[i - 1]) > k) {
 
            // Get the absolute difference
            int absDiff = abs(arr[i] - arr[i - 1]);
 
            // The value which needs to
            // be added or subtracted
            int currOp = absDiff - k;
 
            // Add value to arr[i]
            if (arr[i] < arr[i - 1])
                arr[i] += currOp;
 
            // Subtract value from arr[i]
            else
                arr[i] -= currOp;
 
            // Update the operations
            operations += currOp;
        }
    }
 
    return operations;
}
 
// Driver code
int main()
{
 
    int arr[] = { 1, 2, 5, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
 
    cout << countOp(arr, n, k);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
     
class GFG
{
 
// Function to return the count of
// operations required to update
// the array such that it is possible
// to move from index 0 to index n - 1
static int countOp(int arr[], int n, int k)
{
    int operations = 0;
    for (int i = 1; i < n; i++)
    {
 
        // Current element needs to be updated
        if (Math.abs(arr[i] - arr[i - 1]) > k)
        {
 
            // Get the absolute difference
            int absDiff = Math.abs(arr[i] - arr[i - 1]);
 
            // The value which needs to
            // be added or subtracted
            int currOp = absDiff - k;
 
            // Add value to arr[i]
            if (arr[i] < arr[i - 1])
                arr[i] += currOp;
 
            // Subtract value from arr[i]
            else
                arr[i] -= currOp;
 
            // Update the operations
            operations += currOp;
        }
    }
    return operations;
}
 
// Driver code
static public void main (String []arg)
{
    int arr[] = { 1, 2, 5, 9 };
    int n = arr.length;
    int k = 2;
 
    System.out.println(countOp(arr, n, k));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to return the count of
# operations required to update
# the array such that it is possible
# to move from index 0 to index n - 1
def countOp(arr, n, k) :
 
    operations = 0;
    for i in range(1, n) :
 
        # Current element needs to be updated
        if (abs(arr[i] - arr[i - 1]) > k) :
 
            # Get the absolute difference
            absDiff = abs(arr[i] - arr[i - 1]);
 
            # The value which needs to
            # be added or subtracted
            currOp = absDiff - k;
 
            # Add value to arr[i]
            if (arr[i] < arr[i - 1]) :
                arr[i] += currOp;
 
            # Subtract value from arr[i]
            else :
                arr[i] -= currOp;
 
            # Update the operations
            operations += currOp;
 
    return operations;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 2, 5, 9 ];
    n = len(arr);
    k = 2;
 
    print(countOp(arr, n, k));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
// Function to return the count of
// operations required to update
// the array such that it is possible
// to move from index 0 to index n - 1
static int countOp(int []arr, int n, int k)
{
    int operations = 0;
    for (int i = 1; i < n; i++)
    {
 
        // Current element needs to be updated
        if (Math.Abs(arr[i] - arr[i - 1]) > k)
        {
 
            // Get the absolute difference
            int absDiff = Math.Abs(arr[i] -
                                   arr[i - 1]);
 
            // The value which needs to
            // be added or subtracted
            int currOp = absDiff - k;
 
            // Add value to arr[i]
            if (arr[i] < arr[i - 1])
                arr[i] += currOp;
 
            // Subtract value from arr[i]
            else
                arr[i] -= currOp;
 
            // Update the operations
            operations += currOp;
        }
    }
    return operations;
}
 
// Driver code
static public void Main (String []arg)
{
    int []arr = { 1, 2, 5, 9 };
    int n = arr.Length;
    int k = 2;
 
    Console.WriteLine(countOp(arr, n, k));
}
}
     
// This code is contributed by Rajput-Ji


Javascript


输出:
4