Number of operations to reduce Kth element to 0
Last Updated :
23 Mar, 2024
Given an array arr[] of size N and an integer K, the task is to find the number of operations to reduce the Kth element(0-indexed) to 0. In one operation, the front element, that is arr[0] is decremented by 1 and is removed from the front of the array. If the removed element is still greater than 0, then it is pushed to the back of the array.
Input: N = 3, arr[] = {2, 3, 2}, K = 2
Output: 6
Explanation:
- After applying the first operation, arr[0] is decremented and pushed to the back, so arr[] becomes {3, 2, 1}.
- After applying the second operation, arr[0] is decremented and pushed to the back, so arr[] becomes {2, 1, 2}.
- After applying the third operation, arr[0] is decremented and pushed to the back, so arr[] becomes {1, 2, 1}.
- After applying the fourth operation, arr[0] is decremented and removed from arr[], so arr[] becomes {2, 1}.
- After applying the fifth operation, arr[0] is decremented and pushed to the back, so arr[] becomes {1, 1}.
- After applying the sixth operation, arr[0] is decremented and removed from arr[], so arr[] becomes {1}.
A total of 6 operations are needed to reduce arr[2] to 0.
Input: N = 4, arr[] = {2, 3, 3, 4}, K = 0
Output: 5
Explanation:
- After applying the first operation, arr[0] is decremented and pushed to the back, so arr[] becomes {3, 3, 4, 1}.
- After applying the second operation, arr[0] is decremented and pushed to the back, so arr[] becomes {3, 4, 1, 2}.
- After applying the third operation, arr[0] is decremented and pushed to the back, so arr[] becomes {4, 1, 2, 2}.
- After applying the fourth operation, arr[0] is decremented and pushed to the back, so arr[] becomes {1, 2, 2, 3}.
- After applying the fifth operation, arr[0] is decremented and removed from arr[], so arr[] becomes {2, 2, 3}.
A total of 5 operations are needed to reduce arr[0] to 0.
Approach: To solve the problem, follow the below idea:
The problem can be solved by counting the contribution of all the elements till the Kth element becomes 0. Now, according to the index of any element and the element's value, we can have 4 cases:
- Case 1: (i < K) && (arr[i] <= arr[K]): For all the elements which are present before the Kth element and are less than or equal to the Kth element, these elements would become 0 before the Kth element becomes 0. So, they will contribute arr[i] operations to the final answer.
- Case 2: (i < K) && (arr[i] > arr[K]): For all the elements which are present before the Kth element and are greater than the Kth element, these elements would never become 0 as the Kth element would become 0 before them. So, they will contribute arr[K] operations to the final answer.
- Case 3: (i > K) && (arr[i] < arr[K]): For all the elements which are present after the Kth element and are less than the Kth element, so these elements would become 0 before the Kth element becomes 0. So, they will contribute arr[i] operations to the final answer.
- Case 4: (i > K) && (arr[i] >= arr[K]): For all the elements which are present after the Kth element and are less than the Kth element, so these elements would never become 0 as the Kth element would become 0 before them. So, they will contribute (arr[K] - 1) operations to the final answer.
Now, we can iterate over the array and take the sum of all the operations to get the final answer.
Illustration:
Let's take an example to understand the technique.
Eg: [4, 2, 3, 5, 1], K = 2. Now let's write down each of the rounds.
- After Round 1, arr[] = {3, 1, 2, 4, 0}, which requires 5 seconds.
- After Round 2, arr[] = {2, 0, 1, 3, 0}, which requires 4 seconds.
We can see that the 1st person has a contribution of 3 seconds. The 2nd person has a contribution of 2 seconds. The 3rd person i.e. K = 2nd person has 3 seconds. And the 4th person has contribution of 2 seconds. But whereas the 5th person has only 1 second contribution. So, the answer for this test case is = 3 + 2 + 3 + 2 + 1 = 11 seconds.
So, we can say that for any index i,
- if ( i <= K ) then the contribution is min(arr[K], arr[i])
- if ( i > k ) it will have a contribution of min( arr[K]-1, arr[i])
Step-by-step algorithm:
- Iterate over the entire array arr[],
- If the index < K, then add min(arr[K], arr[i]) to the final answer.
- If the index == K, continue
- If the index > K, then add min(arr[K] - 1, arr[i]) to the final answer.
- After iterating over the entire array, print the final answer.
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the total number of operations to
// reduce the Kth element to 0
int countOperations(vector<int>& arr, int K)
{
// Initialize a result
int ans = 0;
// Size of arr
int n = arr.size();
// for the i <= k indices
// all the elements contributes min(arr[k], arr[i])
for (int i = 0; i <= K; i++)
ans += min(arr[K], arr[i]);
// for the i > k indices
// all the elements contributes min(arr[k], arr[i])
for (int i = K + 1; i < n; i++)
ans += min(arr[K] - 1, arr[i]);
return ans;
}
// Driver Code
int main()
{
vector<int> arr = { 4, 2, 3, 5, 1 };
int K = 2;
cout << countOperations(arr, K);
}
// this code is contributed by Rajdeep Mallick(rajdeep999)
Java
import java.util.Arrays;
import java.util.List;
public class CountOperations {
// Function to calculate the total number of operations to
// reduce the Kth element to 0
static int countOperations(List<Integer> arr, int K) {
// Initialize a result
int ans = 0;
// Size of arr
int n = arr.size();
// For the i <= k indices, all the elements contribute min(arr[k], arr[i])
for (int i = 0; i <= K; i++)
ans += Math.min(arr.get(K), arr.get(i));
// For the i > k indices, all the elements contribute min(arr[k], arr[i] - 1)
for (int i = K + 1; i < n; i++)
ans += Math.min(arr.get(K) - 1, arr.get(i));
return ans;
}
// Driver Code
public static void main(String[] args) {
List<Integer> arr = Arrays.asList(4, 2, 3, 5, 1);
int K = 2;
System.out.println(countOperations(arr, K));
}
}
// This code is contributed by akshitaguprzj3
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
// Function to calculate the total number of operations
// to reduce the Kth element to 0
static int CountOperations(List<int> arr, int K)
{
// Initialize a result
int ans = 0;
// Size of arr
int n = arr.Count;
// for the i <= k indices
// all the elements contributes min(arr[k], arr[i])
for (int i = 0; i <= K; i++)
ans += Math.Min(arr[K], arr[i]);
// for the i > k indices
// all the elements contributes min(arr[k], arr[i])
for (int i = K + 1; i < n; i++)
ans += Math.Min(arr[K] - 1, arr[i]);
return ans;
}
// Driver Code
static void Main(string[] args)
{
List<int> arr = new List<int>{ 4, 2, 3, 5, 1 };
int K = 2;
Console.WriteLine(CountOperations(arr, K));
}
}
// This code is contributed by Susobhan Akhuli
JavaScript
// Function to calculate the total number of operations to
// reduce the Kth element to 0
function countOperations(arr, K) {
// Initialize a result
let ans = 0;
// Size of arr
let n = arr.length;
// for the i <= k indices
// all the elements contributes min(arr[k], arr[i])
for (let i = 0; i <= K; i++)
ans += Math.min(arr[K], arr[i]);
// for the i > k indices
// all the elements contributes min(arr[k], arr[i])
for (let i = K + 1; i < n; i++)
ans += Math.min(arr[K] - 1, arr[i]);
return ans;
}
// Driver Code
let arr = [4, 2, 3, 5, 1];
let K = 2;
console.log(countOperations(arr, K));
Python3
# Python program for the above approach
def count_operations(arr, K):
# Initialize a result
ans = 0
# Size of arr
n = len(arr)
# for the i <= k indices
# all the elements contribute min(arr[k], arr[i])
for i in range(K + 1):
ans += min(arr[K], arr[i])
# for the i > k indices
# all the elements contribute min(arr[k], arr[i])
for i in range(K + 1, n):
ans += min(arr[K] - 1, arr[i])
return ans
# Driver Code
arr = [4, 2, 3, 5, 1]
K = 2
print(count_operations(arr, K))
# This code is contributed by Susobhan Akhuli
Time Complexity: O(N), where N is the size of the array arr[].
Auxiliary Space: O(1)
Similar Reads
Reduce N to 1 with minimum number of given operations Given an integer N, the task is to reduce N to 1 with the following two operations: 1 can be subtracted from each of the digits of the number only if the digit is greater than 0 and the resultant number doesn't have any leading 0s.1 can be subtracted from the number itself. The task is to find the m
6 min read
Minimum number of operations required to reduce N to 1 Given an integer element 'N', the task is to find the minimum number of operations that need to be performed to make 'N' equal to 1. The allowed operations to be performed are: Decrement N by 1.Increment N by 1.If N is a multiple of 3, you can divide N by 3. Examples: Input: N = 4 Output: 2 4 - 1 =
13 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
Minimum steps to reduce N to 0 by given operations Give an integer N, the task is to find the minimum number of moves to reduce N to 0 by one of the following operations: Reduce N by 1.Reduce N to (N/2), if N is divisible by 2.Reduce N to (N/3), if N is divisible by 3. Examples: Input: N = 10Output: 4Explanation: Here N = 10Step 1: Reducing N by 1 i
11 min read
Minimum number of operations required to delete all elements of the array Given an integer array arr, the task is to print the minimum number of operations required to delete all elements of the array. In an operation, any element from the array can be chosen at random and every element divisible by it can be removed from the array. Examples: Input: arr[] = {2, 4, 6, 3, 5
9 min read