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[2] = arr[2] – 1
Operation 2: arr[3] = arr[3] – 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
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 <bits/stdc++.h> 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 |
4
Recommended Posts:
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Search an element in a sorted and rotated array
- Merge an array of size n into another array of size m+n
- k largest(or smallest) elements in an array | added Min Heap method
- Program for array rotation
- Write a program to reverse an array or string
- Reversal algorithm for array rotation
- Block swap algorithm for array rotation
- Leaders in an array
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Find the smallest and second smallest elements in an array
- Maximum and minimum of an array using minimum number of comparisons
- Check for Majority Element in a sorted array
- Segregate 0s and 1s in an array
- Ceiling in a sorted array
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.



