Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 09 Apr, 2021

Given an array of non-negative integers. We need to construct given array from an array of all zeros. We are allowed to do following operation. 

  • Choose any index of say i and add 1 to all the elements or subtract 1 from all the elements from index i to last index. We basically increase/decrease a suffix by 1.

Examples : 
 

Input : brr[] = {1, 2, 3, 4, 5}
Output : 5
Here, we can successively choose indices 1, 2, 
3, 4, and 5, and add 1 to corresponding suffixes.

Input : brr[] = {1, 2, 2, 1}
Output : 3
Here, we choose indices 1 and 2 and adds 1 to 
corresponding suffixes, then we choose index 4 
and subtract 1.

 

Let brr[] be given array and arr[] be current array (which is initially 0).
The approach is simple: 
 

  • To make first element equal we have to make |brr[1]| operations. Once this is done, arr[2], arr[3], arr[4], … arr[n] = brr[1].
  • To make Second element equal we have to make |brr[2] – brr[1]| operations. Once this is done, arr[3], arr[4], arr[5], … arr[n] = brr[2].

In general, to make arr[i] = brr[i] we need to make |brr[i] – b[i – 1]| operations. So in total we have to make |b[1]| + |b[2] – b[1]| + |b[3] – b[2]| + … + |b[n] – b[n – 1]| operations.
Below is CPP and Java implementation of the above approach:
 

C++




// CPP program to find minimum number of steps
// to make the array equal to the given array.
#include <bits/stdc++.h>
using namespace std;
 
// function to calculate min_Steps
int minSteps(int arr[], int n)
{
    int min_Steps = 0;
    for (int i = 0; i < n; i++) {
        if (i > 0)
            min_Steps += abs(arr[i] - arr[i - 1]);
         
        // first element of arr.
        else
            min_Steps += abs(arr[i]);
    }
    return min_Steps;
}
 
// driver function
int main()
{
    int arr[] = { 1, 2, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minSteps(arr, n) << endl;
}

Java




// Java program to find minimum number of steps
// to make the array equal to the given array.
import java.util.*;
import java.lang.*;
 
public class GfG {
    // function to calculate min_Steps
    public static int minSteps(int arr[], int n)
    {
        int min_Steps = 0;
        for (int i = 0; i < n; i++) {
            if (i > 0)
                min_Steps +=
                    Math.abs(arr[i] - arr[i - 1]);
             
            // first element of arr.
            else
                min_Steps += Math.abs(arr[i]);
        }
        return min_Steps;
    }
 
    // driver function
    public static void main(String argc[])
    {
        int[] arr = new int[] { 1, 2, 2, 1 };
        int n = 4;
        System.out.println(minSteps(arr, n));
    }
}

Python3




# Python 3 program to find minimum number
# of steps to make the array equal to the
# given array.
 
# function to calculate min_Steps
def minSteps(arr, n):
    min_Steps = 0
    for i in range(n):
        if (i > 0):
            min_Steps += abs(arr[i] -
                             arr[i - 1])
         
        # first element of arr.
        else:
            min_Steps += abs(arr[i])
    return min_Steps
 
# Driver Code
if __name__ == '__main__':
    arr = [ 1, 2, 2, 1 ]
    n = len(arr)
    print(minSteps(arr, n))
 
# This code is contributed
# by PrinciRaj19992

C#




// C# program to find minimum number of steps
// to make the array equal to the given array.
using System;
 
public class GfG {
     
    // function to calculate min_Steps
    public static int minSteps(int[] arr, int n)
    {
        int min_Steps = 0;
        for (int i = 0; i < n; i++) {
            if (i > 0)
                min_Steps += Math.Abs(arr[i] - arr[i - 1]);
 
            // first element of arr.
            else
                min_Steps += Math.Abs(arr[i]);
        }
        return min_Steps;
    }
 
    // driver function
    public static void Main()
    {
        int[] arr = new int[] { 1, 2, 2, 1 };
        int n = 4;
        Console.WriteLine(minSteps(arr, n));
    }
}
 
// This code is contributed by vt_m

PHP




<?php
// PHP program to find minimum
// number of steps to make the
// array equal to the given array.
 
// function to calculate min_Steps
function minSteps($arr, $n)
{
    $min_Steps = 0;
    for ($i = 0; $i < $n; $i++)
    {
        if ($i > 0)
            $min_Steps += abs($arr[$i] -
                              $arr[$i - 1]);
         
        // first element of arr.
        else
            $min_Steps += abs($arr[$i]);
    }
    return $min_Steps;
}
 
// Driver Code
$arr = array( 1, 2, 2, 1 );
$n = sizeof($arr) ;
 
echo minSteps($arr, $n),"\n";
 
// This code is contributed by ajit
?>

Javascript




<script>
    // Javascript program to find minimum number of steps
    // to make the array equal to the given array.
     
    // function to calculate min_Steps
    function minSteps(arr, n)
    {
        let min_Steps = 0;
        for (let i = 0; i < n; i++) {
            if (i > 0)
                min_Steps += Math.abs(arr[i] - arr[i - 1]);
 
            // first element of arr.
            else
                min_Steps += Math.abs(arr[i]);
        }
        return min_Steps;
    }
     
    let arr = [ 1, 2, 2, 1 ];
    let n = arr.length;
    document.write(minSteps(arr, n));
     
    // This code is contributed by divyeshrabadiya07.
</script>

Output : 
 

3

Time complexity = O(n).
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!