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

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.

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:



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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>
chevron_right


Output :
3

Time complexity = O(n).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Intern at GeeksforGeeks

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.



Improved By : jit_t, princiraj1992

Article Tags :
Practice Tags :