Open In App

# Minimum steps to change arr[K] to 0 by decrementing arr and shifting to end repeatedly

Given an array arr[] of size N and an integer representing an index K, the task is to find the minimum number of operations in which arr[K] becomes 0. In one operation, the value of the first array element decreases by 1 and goes to the end of the array. If at any time, arr[i] becomes 0, then it is removed from the array and the operations are performed on the remaining elements.

Examples:

Input:  arr[] = {2, 3, 2}, K = 2
Output: 6
Explanation: For the first input,
After iteration-1, the array changes to [1, 2, 1]. Steps taken = 3 steps
After iteration-2, the array changes to [0, 1, 0]. Steps taken = 3 steps
Hence, for the element at index 2, it took 6 steps to become 0.

Input:  arr[] = {5, 1, 1, 1}, K = 0
Output: 8

Approach: The idea is to keep traversing the array and decrease the value of arr[i] when it’s greater than 0 and calculate the answer. Follow the steps below to solve the problem:

• Initialize the variable time as 0 to store the answer.
• Traverse in a while loop till arr[k] is not 0 and perform the following tasks:
• Iterate over the range [0, N) using the variable i and perform the following tasks:
• If arr[i] is greater than 0, then reduce the value of arr[i] by 1, then increase the value of time by 1.
• If arr[k] becomes 0, then break.
• After performing the above steps, print the value of time as the answer.

Below is the implementation of the above approach.

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the minimum number``// of steps``void` `findMinimumNumberOfSteps(vector<``int``> arr,``                              ``int` `K)``{` `    ``// Variable to store the answer``    ``int` `time` `= 0;` `    ``// Traverse in the while loop``    ``while` `(arr[K] != 0) {` `        ``// Iterate over the loop``        ``for` `(``int` `i = 0; i < arr.size(); i++) {` `            ``// Check the condition and``            ``// decrease the value``            ``if` `(arr[i] > 0) {``                ``arr[i] -= 1;``                ``time``++;``            ``}` `            ``// Break the loop``            ``if` `(arr[K] == 0)``                ``break``;``        ``}``    ``}` `    ``// Print the result``    ``cout << ``time``;``}` `// Driver Code``int` `main()``{``    ``vector<``int``> arr = { 2, 3, 2 };``    ``int` `K = 2;``    ``findMinimumNumberOfSteps(arr, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `class` `GFG {` `  ``// Function to find the minimum number``  ``// of steps``  ``static` `void` `findMinimumNumberOfSteps(``int` `arr[],``                                       ``int` `K)``  ``{` `    ``// Variable to store the answer``    ``int` `time = ``0``;` `    ``// Traverse in the while loop``    ``while` `(arr[K] != ``0``) {` `      ``// Iterate over the loop``      ``for` `(``int` `i = ``0``; i < arr.length; i++) {` `        ``// Check the condition and``        ``// decrease the value``        ``if` `(arr[i] > ``0``) {``          ``arr[i] -= ``1``;``          ``time++;``        ``}` `        ``// Break the loop``        ``if` `(arr[K] == ``0``)``          ``break``;``      ``}``    ``}` `    ``// Print the result``    ``System.out.println(time);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main (String[] args) {``    ``int` `arr[] = { ``2``, ``3``, ``2` `};``    ``int` `K = ``2``;``    ``findMinimumNumberOfSteps(arr, K);``  ``}``}` `// This code is contributed by hrithikgarg03188.`

## Python3

 `# Python program to implement``# the above approach` `# Function to find the minimum number``# of steps``def` `findMinimumNumberOfSteps(arr, K) :` `    ``# Variable to store the answer``    ``time ``=` `0` `    ``# Traverse in the while loop``    ``while` `(arr[K] !``=` `0``) :` `        ``# Iterate over the loop``        ``for` `i ``in` `range``(``0``, ``len``(arr)) :``            ` `            ``# Check the condition and``            ``# decrease the value``            ``if` `(arr[i] > ``0``) :``                ``arr[i] ``-``=` `1``                ``time ``+``=` `1``            `  `            ``# Break the loop``            ``if` `(arr[K] ``=``=` `0``):``                ``break` `    ``# Print the result``    ``print``(time)` `# Driver Code``arr ``=` `[ ``2``, ``3``, ``2` `]``K ``=` `2``findMinimumNumberOfSteps(arr, K)` `# This code is contributed by sanjoy_62.`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG {` `  ``// Function to find the minimum number``  ``// of steps``  ``static` `void` `findMinimumNumberOfSteps(``int` `[]arr,``                                       ``int` `K)``  ``{` `    ``// Variable to store the answer``    ``int` `time = 0;` `    ``// Traverse in the while loop``    ``while` `(arr[K] != 0) {` `      ``// Iterate over the loop``      ``for` `(``int` `i = 0; i < arr.Length; i++) {` `        ``// Check the condition and``        ``// decrease the value``        ``if` `(arr[i] > 0) {``          ``arr[i] -= 1;``          ``time++;``        ``}` `        ``// Break the loop``        ``if` `(arr[K] == 0)``          ``break``;``      ``}``    ``}` `    ``// Print the result``    ``Console.WriteLine(time);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main () {``    ``int` `[]arr = { 2, 3, 2 };``    ``int` `K = 2;``    ``findMinimumNumberOfSteps(arr, K);``  ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`6`

Time Complexity: O(N*X), where X is the value of arr[K]
Auxiliary Space: O(1)