Reduce the Array to 0 by decreasing elements by 1 or replacing at most K elements by 0 Last Updated : 14 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of N integers and a positive integer K, the task is to find the minimum number of operations required to reduce all array elements to 0 such that in each operation reduce any array element by 1 and independently at most K array element can be reduced to 0. Examples: Input: arr[] = {4, 1, 5}, K = 1Output: 5Explanation: Following are the operations performed to convert all array elements to 0:Here K = 1, So replace arr[2] by 0, converts arr[] to {4, 1, 0} --> Number of operations = 0.Decrease arr[1] by 1, converts arr[] to {4, 0, 0} --> Number of operations = 1.Decrease arr[0] by 1 four times, converts arr[] to {0, 0, 0} --> Number of operations = 4.Therefore, total number of operations = 0 + 1 + 4 = 5, which is minimum possible. Input: arr[] = {4, 2, 10, 9, 18}, K = 2Output: 15 Approach: The given problem can be solved by using the Greedy Approach which is based on the idea is that first sort the given array arr[] in non-decreasing order and then update the K largest element to 0 and perform the given operation on the remaining array elements. Follow the steps below to solve the given problem: If the value of N is at most K, then replace all array elements with 0. Therefore the number of operations required in this case will be 0.Sort the array arr[] in non-decreasing order.Replace last K elements by 0.Print the sum of the first (N - K) elements as the resultant count of operations. 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 the minimum number // operations needed to convert all the // array elements to 0 int minOperations(vector<int> arr, int K, int N) { // If K is greater than 0 then // replace all array elements to 0 if (K >= N) return 0; // Sort array in non-decreasing order sort(arr.begin(), arr.end()); // Stores the count of operations // required int countOperations = 0; // Iterate loop until N - K times for (int i = 0; i < N - K; i++) { // Take sum of elements countOperations += arr[i]; } // Return countOperations as // the required answer return countOperations; } // Driver Code int main() { vector<int> arr{ 4, 1, 5 }; int N = arr.size(); int K = 1; cout << minOperations(arr, K, N); return 0; } Java // Java program for the above approach import java.util.Arrays; public class GFG { // Function to find the minimum number // operations needed to convert all the // array elements to 0 static int minOperations(int []arr, int K, int N) { // If K is greater than 0 then // replace all array elements to 0 if (K >= N) return 0; // Sort array in non-decreasing order Arrays.sort(arr) ; // Stores the count of operations // required int countOperations = 0; // Iterate loop until N - K times for (int i = 0; i < N - K; i++) { // Take sum of elements countOperations += arr[i]; } // Return countOperations as // the required answer return countOperations; } // Driver Code public static void main (String[] args) { int[] arr = { 4, 1, 5 }; int N = arr.length; int K = 1; System.out.println(minOperations(arr, K, N)); } } // This code is contributed by AnkThon Python3 # Python3 program for the above approach # Function to find the minimum number # operations needed to convert all the # array elements to 0 def minOperations(arr, K, N) : # If K is greater than 0 then # replace all array elements to 0 if (K >= N) : return 0; # Sort array in non-decreasing order arr.sort(); # Stores the count of operations # required countOperations = 0; # Iterate loop until N - K times for i in range(N - K) : # Take sum of elements countOperations += arr[i]; # Return countOperations as # the required answer return countOperations; # Driver Code if __name__ == "__main__" : arr = [ 4, 1, 5 ]; N = len(arr); K = 1; print(minOperations(arr, K, N)); # This code is contributed by AnkThon C# // C# program for the above approach using System; public class GFG { // Function to find the minimum number // operations needed to convert all the // array elements to 0 static int minOperations(int []arr, int K, int N) { // If K is greater than 0 then // replace all array elements to 0 if (K >= N) return 0; // Sort array in non-decreasing order Array.Sort(arr) ; // Stores the count of operations // required int countOperations = 0; // Iterate loop until N - K times for (int i = 0; i < N - K; i++) { // Take sum of elements countOperations += arr[i]; } // Return countOperations as // the required answer return countOperations; } // Driver Code public static void Main (string[] args) { int[] arr = { 4, 1, 5 }; int N = arr.Length; int K = 1; Console.WriteLine(minOperations(arr, K, N)); } } // This code is contributed by AnkThon JavaScript <script> // Javascript program for the above approach // Function to find the minimum number // operations needed to convert all the // array elements to 0 function minOperations(arr, K, N) { // If K is greater than 0 then // replace all array elements to 0 if (K >= N) return 0; // Sort array in non-decreasing order arr.sort((a, b) => a - b); // Stores the count of operations // required let countOperations = 0; // Iterate loop until N - K times for (let i = 0; i < N - K; i++) { // Take sum of elements countOperations += arr[i]; } // Return countOperations as // the required answer return countOperations; } // Driver Code let arr = [4, 1, 5]; let N = arr.length; let K = 1; document.write(minOperations(arr, K, N)); // This code is contributed by saurabh_jaiswal. </script> Output: 5 Time Complexity: O(N*log N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Reduce the Array to 0 by decreasing elements by 1 or replacing at most K elements by 0 hrithikgarg03188 Follow Improve Article Tags : Misc Greedy Sorting Mathematical DSA Arrays +2 More Practice Tags : ArraysGreedyMathematicalMiscSorting +1 More Similar Reads Check if sum of the given array can be reduced to 0 by reducing array elements by K Given an array arr[] consisting of N integers and an integer K, the task is to check if the sum of the array can be reduced to 0 by subtracting array elements by K any number of times. Examples: Input: arr[ ]= {-3, 2, -1, 5, 1}, K=2Output: "Yes"Explanation: Sum of the array is 4. Therefore, decreasi 4 min read Minimize cost to reduce array to a single element by replacing K consecutive elements by their sum Given an array arr[] of size N and an integer K, the task is to find the minimum cost required to reduce given array to a single element, where cost of replacing K consecutive array elements by their sum is equal to the sum of the K consecutive elements. If it is not possible to reduce given array t 10 min read Remove array elements to reduce frequency of each array element to at most K Given a sorted array arr[] of size N and an integer K, the task is to print the array by removing the minimum number of array elements to make frequency of each element at most K. Examples: Input: arr[] = { 1, 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5 }, K = 3 Output: { 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5 } Ex 14 min read Minimum operations to make Array elements 0 by decrementing pair or single element Given an array arr[] of size N, the task is to find the minimum number of operations required to reduce all three elements of the array to zero. Following operations are allowed: Reduce 2 different array elements by one.Reduce a single array element by one.Example:Input: arr[] = {1, 2, 3}, N = 3Outp 11 min read Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum Given an array arr[] consisting of N integers, the task is to check if it is possible to reduce the array elements to 0 by repeatedly subtracting the minimum of any pair of consecutive array elements from both the elements in the pair. If it is possible, then print "Yes". Otherwise, print "No". Exam 10 min read Minimize the maximum of Array by replacing any element with other element at most K times Given an array arr[] of size N and an integer K, the task is to minimize the value of the maximum element of the array arr[] after replacing any element of the array with any other element of that array at most K times.Examples:Input: arr[] = {5, 3, 3, 2, 1}, K = 3Output: 2Explanation: Replace the e 8 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 Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements Given an array arr[] of size N, the task is to make all array elements equal to 0 by replacing all elements of a subsequences of equal elements by any integer, minimum number of times. Examples: Input: arr[] = {3, 7, 3}, N = 3Output: 2Explanation:Selecting a subsequence { 7 } and replacing all its e 5 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 Modify array by making all array elements equal to 0 by subtracting K^i from an array element in every i-th step Given an array arr[] of size N, the task is to check if it is possible to convert all array elements to 0s, by subtracting Ki from an array element, in the ith step. If it is possible to do so, then print "Yes". Otherwise, print "No". Examples: Input: N = 5, K = 2, arr[] = {8, 0, 3, 4, 80}Output: Ye 8 min read Like