Skip to content
Related Articles

Related Articles

Count of operations required to update the array such that it satisfies the given conditions
  • Difficulty Level : Easy
  • Last Updated : 17 Mar, 2021

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:
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:
 

 

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

Javascript




<script>
 
    // Javascript 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
    function countOp(arr, n, k)
    {
 
        let operations = 0;
        for (let i = 1; i < n; i++) {
 
            // Current element needs to be updated
            if (Math.abs(arr[i] - arr[i - 1]) > k) {
 
                // Get the absolute difference
                let absDiff = Math.abs(arr[i] - arr[i - 1]);
 
                // The value which needs to
                // be added or subtracted
                let 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;
    }
     
    let arr = [ 1, 2, 5, 9 ];
    let n = arr.length;
    let k = 2;
   
    document.write(countOp(arr, n, k));
     
</script>
Output: 
4

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :