Minimum number of steps required to obtain the given Array by the given operations

Given an array arr[] of N positive integers, the task is to find the minimum number of operations required of the following types to obtain the array arr[] from an array of zeroes only.

Examples: 
 

Input: arr[]={1,1,2,2,1} 
Output:
Explanation: 
Initially arr[] = {0,0,0,0,0} 
Step 1: arr[]={1,1,1,1,1}(Operation 1 on index 0) 
Step 2: arr[]={1,1,2,2,2}(Operation 1 on index 2) 
Step 3: arr[]={1,1,2,2,1}(Operation 2 on index 4)
Input: arr[]={1,2,3,4} 
Output:
 

Naive Approach: The simplest approach is to convert each element of the array result array, brr[] to arr[] by performing one of the above operations over the indices [i, N – 1] and increase the count of each operation performed. 
Time Complexity: O(N2
Auxiliary Space: O(1)
Efficient Approach: 
The above approach can be optimized using Greedy Approach
Follow the steps below to solve the problem: 
 

Below is the implementation of the above approach:
 



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the minimum
// steps to obtain the desired array
int min_operation(int a[], int n)
{
    // Initialize variable
    int ans = 0;
 
    // Iterate over the array arr[]
    for (int i = 0; i < n; i++) {
 
        // Check if i > 0
        if (i > 0)
 
            // Update the answer
            ans += abs(a[i] - a[i - 1]);
 
        else
            ans += abs(a[i]);
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << min_operation(arr, n);
 
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to calculate the minimum
// steps to obtain the desired array
static int min_operation(int a[], int n)
{
    // Initialize variable
    int ans = 0;
 
    // Iterate over the array arr[]
    for (int i = 0; i < n; i++)
    {
 
        // Check if i > 0
        if (i > 0)
 
            // Update the answer
            ans += Math.abs(a[i] - a[i - 1]);
 
        else
            ans += Math.abs(a[i]);
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4 };
    int n = arr.length;
 
    System.out.print(min_operation(arr, n));
}
}
 
// This code is contributed by gauravrajput1
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to calculate the minimum
# steps to obtain the desired array
def min_operation(a, n):
 
    # Initialize variable
    ans = 0
 
    # Iterate over the array arr[]
    for i in range(n):
 
        # Check if i > 0
        if (i > 0):
 
            # Update the answer
            ans += abs(a[i] - a[i - 1])
        else:
            ans += abs(a[i])
 
    # Return the result
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 1, 2, 3, 4 ]
    n = len(arr)
 
    print(min_operation(arr, n))
 
# This code is contributed by chitranayal
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to implement
// the above approach
using System;
class GFG{
 
// Function to calculate the minimum
// steps to obtain the desired array
static int min_operation(int []a, int n)
{
    // Initialize variable
    int ans = 0;
 
    // Iterate over the array []arr
    for (int i = 0; i < n; i++)
    {
 
        // Check if i > 0
        if (i > 0)
 
            // Update the answer
            ans += Math.Abs(a[i] - a[i - 1]);
 
        else
            ans += Math.Abs(a[i]);
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4 };
    int n = arr.Length;
 
    Console.Write(min_operation(arr, n));
}
}
 
// This code is contributed by Amit Katiyar
chevron_right

Output: 
 

4



 

Time Complexity: O(N)
Auxiliary Space: O(1)

 

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.





Code and Let Code

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.



Article Tags :
Practice Tags :