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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum steps
// required to reduce all array
// elements to 1
int minSteps(int arr[], int N)
{
// Variable to store the answer
int steps = 0;
// Vector with all elements initialized
// with 0
vector<long long> v(N + 1, 0);
// Traverse the array
for (int i = 0; i < N; ++i) {
// Variable 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
int k = max(0ll, arr[i] - 1 - v[i]);
// Increment steps by K
steps += k;
// Update element in v
v[i] += k;
// Loop to compute how much current element
// effect the (i+2) to N elements
for (int j = i + 2; j <= min(i + arr[i], N); j++) {
v[j]++;
}
v[i + 1] += v[i] - arr[i] + 1;
}
// Return steps which is the answer
return steps;
}
// Driver Code
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
// Java code for the above approach
import java.io.*;
class GFG
{
// Function to find minimum steps
// required to reduce all array
// elements to 1
static int minSteps(int arr[], int N)
{
// Variable to store the answer
int steps = 0;
// Vector with all elements initialized
// with 0
int v[] = new int[N + 1];
// Traverse the array
for (int i = 0; i < N; ++i)
{
// Variable 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
int k = Math.max(0, arr[i] - 1 - v[i]);
// Increment steps by K
steps += k;
// Update element in v
v[i] += k;
// Loop to compute how much current element
// effect the (i+2) to N elements
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 which is the answer
return steps;
}
// Driver Code
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));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
# Function to find minimum steps
# required to reduce all array
# elements to 1
def minSteps(arr, N):
# Variable to store the answer
steps = 0
# Vector with all elements initialized
# with 0
v = [0] * (N + 1)
# Traverse the array
for i in range(N):
# Variable 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
k = max(0, arr[i] - 1 - v[i])
# Increment steps by K
steps += k
# Update element in v
v[i] += k
# Loop to compute how much current element
# effect the (i+2) to N elements
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 which is the answer
return steps
# Driver Code
arr = [4, 2, 3, 2, 2, 2, 1, 2]
N = len(arr)
print(minSteps(arr, N))
# This code is contributed by gfgking.
C#
// C# code for the above approach
using System;
class GFG
{
// Function to find minimum steps
// required to reduce all array
// elements to 1
static int minSteps(int[] arr, int N)
{
// Variable to store the answer
int steps = 0;
// Vector with all elements initialized
// with 0
int[] v = new int[N + 1];
// Traverse the array
for (int i = 0; i < N; ++i)
{
// Variable 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
int k = Math.Max(0, arr[i] - 1 - v[i]);
// Increment steps by K
steps += k;
// Update element in v
v[i] += k;
// Loop to compute how much current element
// effect the (i+2) to N elements
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 which is the answer
return steps;
}
// Driver Code
public static void Main()
{
int[] arr = { 4, 2, 3, 2, 2, 2, 1, 2 };
int N = arr.Length;
Console.Write(minSteps(arr, N));
}
}
// This code is contributed by gfgking
JavaScript
<script>
// JavaScript program for the above approach
// Function to find minimum steps
// required to reduce all array
// elements to 1
const minSteps = (arr, N) => {
// Variable to store the answer
let steps = 0;
// Vector with all elements initialized
// with 0
let v = new Array(N + 1).fill(0);
// Traverse the array
for (let i = 0; i < N; ++i) {
// Variable 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
let k = Math.max(0, arr[i] - 1 - v[i]);
// Increment steps by K
steps += k;
// Update element in v
v[i] += k;
// Loop to compute how much current element
// effect the (i+2) to N elements
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 which is the answer
return steps;
}
// Driver Code
let arr = [4, 2, 3, 2, 2, 2, 1, 2];
let N = arr.length
document.write(minSteps(arr, N));
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N2)
Auxiliary Space: O(N)
Similar Reads
Minimum steps required to reduce all the elements of the array to zero Given an array arr[] of positive integers, the task is to find the minimum steps to reduce all the elements to 0. In a single step, -1 can be added to all the non-zero elements of the array at the same time.Examples: Input: arr[] = {1, 5, 6} Output: 6 Operation 1: arr[] = {0, 4, 5} Operation 2: arr[
4 min read
Minimum Decrements on Subarrays required to reduce all Array elements to zero Given an array arr[] consisting of N non-negative integers, the task is to find the minimum number of subarrays that needs to be reduced by 1 such that all the array elements are equal to 0. Example: Input: arr[] = {1, 2, 3, 2, 1}Output: 3Explanation: Operation 1: {1, 2, 3, 2, 1} -> {0, 1, 2, 1,
5 min read
Minimum number of given operations required to reduce the array to 0 element Given an array arr[] of N integers. The task is to find the minimum number of given operations required to reduce the array to 0 elements. In a single operation, any element can be chosen from the array and all of its multiples get removed including itself.Examples: Input: arr[] = {2, 4, 6, 3, 4, 6,
6 min read
Minimize steps required to make all array elements same by adding 1, 2 or 5 Given an array arr[] of size N, the task is to count the minimum number of steps required to make all the array elements the same by adding 1, 2, or 5 to exactly (N - 1) elements of the array at each step. Examples: Input: N = 4, arr[] = {2, 2, 3, 7}Output: 2Explanation: Step 1: {2, 2, 3, 7} -> {
11 min read
Minimize subarray increments/decrements required to reduce all array elements to 0 Given an array arr[], select any subarray and apply any one of the below operations on each element of the subarray: Increment by oneDecrement by one The task is to print the minimum number of above-mentioned increment/decrement operations required to reduce all array elements to 0. Examples: Input:
5 min read
Minimum number of decrements by 1 required to reduce all elements of a circular array to 0 Given an circular array arr[] consisting of N integers, the task is to find the minimum number of operations to reduce all elements of a circular array to 0. In each operation, reduce the current element by 1 (starting from the first element) and move to the next element. Examples: Input: arr[] = {2
6 min read
Minimum Subarray flips required to convert all elements of a Binary Array to K The problem statement is asking for the minimum number of operations required to convert all the elements of a given binary array arr[] to a specified value K, where K can be either 0 or 1. The operations can be performed on any index X of the array, and the operation is to flip all the elements of
8 min read
Maximize pair decrements required to reduce all array elements except one to 0 Given an array arr[] consisting of N distinct elements, the task is to find the maximum number of pairs required to be decreased by 1 in each step, such that N - 1 array elements are reduced to 0 and the remaining array element is a non-negative integer. Examples: Input: arr[] = {1, 2, 3}Output: 3Ex
12 min read
Minimum steps to make sum and the product of all elements of array non-zero Given an array arr of N integers, the task is to find the minimum steps in which the sum and product of all elements of the array can be made non-zero. In one step any element of the array can be incremented by 1.Examples: Input: N = 4, arr[] = {0, 1, 2, 3} Output: 1 Explanation: As product of all e
7 min read
Minimum number of array elements from either ends required to be subtracted from X to reduce X to 0 Given an array nums[] and an integer X, the task is to reduce X to 0 by removing either the leftmost or the rightmost array elements and subtracting its value from X, minimum number of times. If it's possible to reduce X to 0, print the count of operations required. Otherwise, return -1. Examples: I
9 min read