# Count of suffix increment/decrement operations to construct a given array in C++

C++Server Side ProgrammingProgramming

We are given a target array arr[] containing positive integers. The goal is to construct the target array arr[] using an initial array with all 0s. The operations that can be applied on a given empty array with all 0s will suffix increment/decrement operations.

If we choose any index say i, then in case of suffix increment operation we will add 1 to all elements from index i till last index.

In case of suffix decrement operation we will subtract 1 from all elements from index i till last index.

Let us understand with examples

Input − arr[]= { 1,2,3 }

Output − Count of suffix increment/decrement operations to construct a given array are − 3

Explanation

Starting from { 0, 0, 0 }
Choose index 0, applying suffix increment { 1, 1, 1 }
Choose index 1, applying suffix increment { 1, 2, 2 }
Choose index 2, applying suffix increment { 1, 2, 3 }
Total operations =3

Input − arr[]= { 1, 4, 5, 3 }

Output − Count of suffix increment/decrement operations to construct a given array are − 7

Explanation

Starting from { 0, 0, 0, 0 }
Choose index 0, applying suffix increment { 1, 1, 1, 1 }
Choose index 1, applying suffix increment { 1, 2, 2, 2 }
Choose index 1, applying suffix increment { 1, 3, 3, 3 }
Choose index 1, applying suffix increment { 1, 4, 4, 4 }
Choose index 2, applying suffix increment { 1, 4, 5, 5 }
Choose index 3, applying suffix decrement { 1, 4, 5, 4 }
Choose index 3, applying suffix decrement { 1, 4, 5, 3 }
Total operations = 7

## Approach used in the below program is as follows

If we take the initial array as B[ ]. To make first element B equal to arr. We will need arr number of suffix increment operations. After this all B=B....=B[n-1]=arr will be equal.

To make second element B equal to arr, we will need | arr-arr | number of operations. (either increment or decrement).

So to make B[i] equal to arr[i] we will need | arr[i]- arr[i-1] | number of operations.

Total count of operations will be | arr | + | arr-arr | + …. + | arr[n-1]-arr[n-2] |.

• Take the target array as arr[].

• Function incr_decr_op(int arr[], int size) takes array and its length and returns the count of suffix increment/decrement operations to construct a given array

• Take the initial count as 0.

• Traverse array arr[] using for loop

• For index 0 add arr[i] to count.

• For other indexes add abs( arr[i]-arr[i-1] ) to count.

• At the end of the for loop return count as result.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int incr_decr_op(int arr[], int size){
int count = 0;
for (int i = 0; i < size; i++){
if (i > 0){
count += abs(arr[i] - arr[i - 1]);
}
else{
count = count + abs(arr[i]);
}
}
return count;
}
int main(){
int arr[] = { 3, 3, 1, 2, 2 };
int size = sizeof(arr) / sizeof(arr);
cout<<"Count of suffix increment/decrement operations to construct a given array are: "<<incr_decr_op(arr, size) << endl;
}

## Output

If we run the above code it will generate the following output −

Count of suffix increment/decrement operations to construct a given array are: 6