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++
#include <bits/stdc++.h>
using namespace std;
void findMinimumNumberOfSteps(vector< int > arr,
int K)
{
int time = 0;
while (arr[K] != 0) {
for ( int i = 0; i < arr.size(); i++) {
if (arr[i] > 0) {
arr[i] -= 1;
time ++;
}
if (arr[K] == 0)
break ;
}
}
cout << time ;
}
int main()
{
vector< int > arr = { 2, 3, 2 };
int K = 2;
findMinimumNumberOfSteps(arr, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void findMinimumNumberOfSteps( int arr[],
int K)
{
int time = 0 ;
while (arr[K] != 0 ) {
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] > 0 ) {
arr[i] -= 1 ;
time++;
}
if (arr[K] == 0 )
break ;
}
}
System.out.println(time);
}
public static void main (String[] args) {
int arr[] = { 2 , 3 , 2 };
int K = 2 ;
findMinimumNumberOfSteps(arr, K);
}
}
|
Python3
def findMinimumNumberOfSteps(arr, K) :
time = 0
while (arr[K] ! = 0 ) :
for i in range ( 0 , len (arr)) :
if (arr[i] > 0 ) :
arr[i] - = 1
time + = 1
if (arr[K] = = 0 ):
break
print (time)
arr = [ 2 , 3 , 2 ]
K = 2
findMinimumNumberOfSteps(arr, K)
|
C#
using System;
class GFG {
static void findMinimumNumberOfSteps( int []arr,
int K)
{
int time = 0;
while (arr[K] != 0) {
for ( int i = 0; i < arr.Length; i++) {
if (arr[i] > 0) {
arr[i] -= 1;
time++;
}
if (arr[K] == 0)
break ;
}
}
Console.WriteLine(time);
}
public static void Main () {
int []arr = { 2, 3, 2 };
int K = 2;
findMinimumNumberOfSteps(arr, K);
}
}
|
Javascript
<script>
function findMinimumNumberOfSteps(arr,
K) {
let time = 0;
while (arr[K] != 0) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
arr[i] -= 1;
time++;
}
if (arr[K] == 0)
break ;
}
}
document.write(time);
}
let arr = [2, 3, 2];
let K = 2;
findMinimumNumberOfSteps(arr, K);
</script>
|
Time Complexity: O(N*X), where X is the value of arr[K]
Auxiliary Space: O(1)