Open In App

Number of operations to reduce Kth element to 0

Last Updated : 23 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

Output
11

Time Complexity: O(N), where N is the size of the array arr[].
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads