# Count of operations required to update the array such that it satisfies the given conditions

Given an array arr[] of size N and an integer K. The task is to find the operations required to update the array such that it is possible to move from index 0 to index N – 1 when any index j can be visited from index i if index j is adjacent to index i and abs(arr[i] – arr[j]) ≤ K. In a single operation, any element of the array can be incremented or decremented by 1.

Examples:

Input: arr[] = {1, 2, 5, 9}, K = 2
Output: 4
Operation 1: arr = arr – 1
Operation 2: arr = arr – 3
The new array becomes arr[] = {1, 2, 4, 6}
which satisfies the given condition.

Input: arr[] = {-2, 0, 1, 4}, K = 5
Output: 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Traverse the array starting from the second element and calculate the absolute difference between the current and the previous element.
• If the absolute difference is greater than K then the current element needs to be updated i.e. add the value to the smaller element or subtract the value from the larger element such that the absolute difference becomes K.

Below is the implementation of the above approach:

## 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); ` `    ``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 `

Output:

```4
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : AnkitRai01, Rajput-Ji

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.