Count of operations required to update the array such that it satisfies the given conditions
Last Updated :
07 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int countOp( int arr[], int n, int k)
{
int operations = 0;
for ( int i = 1; i < n; i++) {
if ( abs (arr[i] - arr[i - 1]) > k) {
int absDiff = abs (arr[i] - arr[i - 1]);
int currOp = absDiff - k;
if (arr[i] < arr[i - 1])
arr[i] += currOp;
else
arr[i] -= currOp;
operations += currOp;
}
}
return operations;
}
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
import java.util.*;
class GFG
{
static int countOp( int arr[], int n, int k)
{
int operations = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (Math.abs(arr[i] - arr[i - 1 ]) > k)
{
int absDiff = Math.abs(arr[i] - arr[i - 1 ]);
int currOp = absDiff - k;
if (arr[i] < arr[i - 1 ])
arr[i] += currOp;
else
arr[i] -= currOp;
operations += currOp;
}
}
return operations;
}
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));
}
}
|
Python3
def countOp(arr, n, k) :
operations = 0 ;
for i in range ( 1 , n) :
if ( abs (arr[i] - arr[i - 1 ]) > k) :
absDiff = abs (arr[i] - arr[i - 1 ]);
currOp = absDiff - k;
if (arr[i] < arr[i - 1 ]) :
arr[i] + = currOp;
else :
arr[i] - = currOp;
operations + = currOp;
return operations;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 5 , 9 ];
n = len (arr);
k = 2 ;
print (countOp(arr, n, k));
|
C#
using System;
class GFG
{
static int countOp( int []arr, int n, int k)
{
int operations = 0;
for ( int i = 1; i < n; i++)
{
if (Math.Abs(arr[i] - arr[i - 1]) > k)
{
int absDiff = Math.Abs(arr[i] -
arr[i - 1]);
int currOp = absDiff - k;
if (arr[i] < arr[i - 1])
arr[i] += currOp;
else
arr[i] -= currOp;
operations += currOp;
}
}
return operations;
}
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));
}
}
|
Javascript
<script>
function countOp(arr, n, k)
{
let operations = 0;
for (let i = 1; i < n; i++) {
if (Math.abs(arr[i] - arr[i - 1]) > k) {
let absDiff = Math.abs(arr[i] - arr[i - 1]);
let currOp = absDiff - k;
if (arr[i] < arr[i - 1])
arr[i] += currOp;
else
arr[i] -= currOp;
operations += currOp;
}
}
return operations;
}
let arr = [ 1, 2, 5, 9 ];
let n = arr.length;
let k = 2;
document.write(countOp(arr, n, k));
</script>
|
Time complexity: O(N) where N is the size of the array
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...