Minimum steps required to reduce all array elements to 1 based on given steps
Last Updated :
22 Nov, 2021
Given an array arr[] of size N. The task is to find the minimum steps required to reduce all array elements to 1. In each step, perform the following given operation:
- Choose any starting index, say i, and jump to the (arr[i] + i)th index, reducing ith as well as (arr[i] + i)th index by 1, follow this process until it reaches the end of the array
- If an element is already reduced to 1, it can’t be reduced more, it remains the same.
Examples:
Input: arr[] = {4, 2, 3, 2, 2, 2, 1, 2}, N = 8
Output: 5
Explanation: Series of operations can be performed in the following way:
- {4, 2, 3, 2, 2, 2, 1, 2}, decrement values by 1, arr[] = {4, 2, 2, 2, 2, 1, 1, 1}
- {4, 2, 2, 2, 2, 1, 1, 1}, decrement values by 1, arr[] = {3, 2, 2, 2, 1, 1, 1, 1}
- {3, 2, 2, 2, 1, 1, 1, 1}, decrement values by 1, arr[] = {2, 2, 2, 1, 1, 1, 1, 1}
- {2, 2, 2, 1, 1, 1, 1, 1}, decrement values by 1, arr[] = {1, 2, 1, 1, 1, 1, 1, 1}
- {1, 2, 1, 1, 1, 1, 1, 1}, decrement values by 1, arr[] = {1, 1, 1, 1, 1, 1, 1, 1}
So, total steps required = 5
Input: arr[] = {1, 3, 1, 2, 2}, N = 5
Output: 2
Approach: The given problem can be solved by dividing the problem in 4 parts :- (0 to i-1) | i | (i + 1) | (i + 2 to n – 1). Follow the below steps to solve the problem:
- Take a vector say v, which will denote how many times an element is decreased due to visiting the previous elements.
- Each element in v i.e v[i] denotes the count of decrement in arr[i] due to visiting the elements from 0 to (i-1).
- Iterate over the array arr[], and take a variable say k to store the numbers of passes that have to add in answer due to element arr[i] after making 0 to (i-1) elements equal to 1.
- Inside the loop, run another loop(i.e the second loop) to compute how much current arr[i] elements effect(decrease after visiting the ith element) the (i+2) to N elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minSteps( int arr[], int N)
{
int steps = 0;
vector< long long > v(N + 1, 0);
for ( int i = 0; i < N; ++i) {
int k = max(0ll, arr[i] - 1 - v[i]);
steps += k;
v[i] += k;
for ( int j = i + 2; j <= min(i + arr[i], N); j++) {
v[j]++;
}
v[i + 1] += v[i] - arr[i] + 1;
}
return steps;
}
int main()
{
int arr[] = { 4, 2, 3, 2, 2, 2, 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minSteps(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int minSteps( int arr[], int N)
{
int steps = 0 ;
int v[] = new int [N + 1 ];
for ( int i = 0 ; i < N; ++i)
{
int k = Math.max( 0 , arr[i] - 1 - v[i]);
steps += k;
v[i] += k;
for ( int j = i + 2 ;
j <= Math.min(i + arr[i], N); j++) {
v[j]++;
}
v[i + 1 ] += v[i] - arr[i] + 1 ;
}
return steps;
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 3 , 2 , 2 , 2 , 1 , 2 };
int N = arr.length;
System.out.println(minSteps(arr, N));
}
}
|
Python3
def minSteps(arr, N):
steps = 0
v = [ 0 ] * (N + 1 )
for i in range (N):
k = max ( 0 , arr[i] - 1 - v[i])
steps + = k
v[i] + = k
for j in range (i + 2 , min (i + arr[i], N) + 1 ):
v[j] + = 1
v[i + 1 ] + = v[i] - arr[i] + 1
return steps
arr = [ 4 , 2 , 3 , 2 , 2 , 2 , 1 , 2 ]
N = len (arr)
print (minSteps(arr, N))
|
C#
using System;
class GFG
{
static int minSteps( int [] arr, int N)
{
int steps = 0;
int [] v = new int [N + 1];
for ( int i = 0; i < N; ++i)
{
int k = Math.Max(0, arr[i] - 1 - v[i]);
steps += k;
v[i] += k;
for ( int j = i + 2;
j <= Math.Min(i + arr[i], N); j++) {
v[j]++;
}
v[i + 1] += v[i] - arr[i] + 1;
}
return steps;
}
public static void Main()
{
int [] arr = { 4, 2, 3, 2, 2, 2, 1, 2 };
int N = arr.Length;
Console.Write(minSteps(arr, N));
}
}
|
Javascript
<script>
const minSteps = (arr, N) => {
let steps = 0;
let v = new Array(N + 1).fill(0);
for (let i = 0; i < N; ++i) {
let k = Math.max(0, arr[i] - 1 - v[i]);
steps += k;
v[i] += k;
for (let j = i + 2; j <= Math.min(i + arr[i], N); j++) {
v[j]++;
}
v[i + 1] += v[i] - arr[i] + 1;
}
return steps;
}
let arr = [4, 2, 3, 2, 2, 2, 1, 2];
let N = arr.length
document.write(minSteps(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...