Minimize Array elements to be reduced to make subsequences sum 1 to Array max possible Last Updated : 25 May, 2022 Comments Improve Suggest changes Like Article Like Report Given an array a[] of positive integers. Subtract any positive integer from any number of elements of the array such that there exists every possible subsequence with sum 1 to s, where s denotes the sum of all the elements of the array. Find the minimum possible number of the array element to be subtracted. Input: a[] = {4, 3, 2, 2, 1, 6}Output: 2Explanation: By subtracting 5 from 6 and 2 from 4 we get array {2, 3, 2, 2, 1, 1} which fulfills required condition. Input: a[] = { 1, 2, 2, 2, 1, 2 }Output: 0 Approach: The required array will be found if s<2*(size of the array) where s denotes the sum of all the elements of the array. If this condition is not satisfied by the array we have to subtract s-((2*size of array )-1) from elements of the given array. Since we have to find the minimum possible number of the array element to be subtracted. We will perform the maximum possible subtraction from the greatest element of the array and repeat the process until we get the required result. Follow the steps below to solve the problem: Initialize the variable sum as the sum of all elements of the array arr[].Initialize the variable diff as sum - (2*size) +1.Initialize the variable ans as 0, i as size-1.Sort the array arr[].Traverse over a while loop till diff is greater than 0 and perform the following tasks:Subtract the value of arr[i]-1 from the variable diff and increase the value of ans by 1.Decrease the value of i by 1.After performing the above steps, print the value of ans as the answer. 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 total number // of operations required int find(int arr[], int size) { int sum = 0; // Find sum of all element of array arr. for (int i = 0; i < size; i++) { sum += arr[i]; } // Variable to store integer which needs // to be subtracted from arr in total. int diff = sum - ((2 * size) - 1); int ans = 0; int i = size - 1; sort(arr, arr + size); // Iteration to calcute total number of // subtraction required to get desired array. while (diff > 0) { diff -= (arr[i] - 1); i--; ans++; } return ans; } // Driver Code int main() { int arr[] = { 4, 3, 2, 2, 1, 6 }; cout << find(arr, 6) << "\n"; return 0; } Java // Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to find the total number // of operations required static int find(int arr[], int size) { int sum = 0; // Find sum of all element of array arr. for (int i = 0; i < size; i++) { sum += arr[i]; } // Variable to store integer which needs // to be subtracted from arr in total. int diff = sum - ((2 * size) - 1); int ans = 0; int i = size - 1; Arrays.sort(arr); // Iteration to calcute total number of // subtraction required to get desired array. while (diff > 0) { diff -= (arr[i] - 1); i--; ans++; } return ans; } // Driver code public static void main (String[] args) { int arr[] = { 4, 3, 2, 2, 1, 6 }; int N = arr.length; System.out.println( find(arr, N)); } } // This code is contributed by hrithikgarg03188 Python # Python program for the above approach # Function to find the total number # of operations required def find(arr, size): sum = 0 # Find sum of all element of array arr. for i in range(0, size): sum = sum + arr[i] # Variable to store integer which needs # to be subtracted from arr in total. diff = sum - ((2 * size) - 1) ans = 0 i = size - 1 arr.sort() # Iteration to calcute total number of # subtraction required to get desired array. while (diff > 0): diff = diff - (arr[i] - 1) i = i - 1 ans = ans + 1 return ans # Driver Code arr = [4, 3, 2, 2, 1, 6] print(find(arr, 6)) # This code is contributed by Taranpreet C# // C# program for the above approach using System; class GFG { // Function to find the total number // of operations required static int find(int[] arr, int size) { int sum = 0; // Find sum of all element of array arr. for (int i = 0; i < size; i++) { sum += arr[i]; } // Variable to store integer which needs // to be subtracted from arr in total. int diff = sum - ((2 * size) - 1); int ans = 0; int j = size - 1; Array.Sort(arr); // Iteration to calcute total number of // subtraction required to get desired array. while (diff > 0) { diff -= (arr[j] - 1); j--; ans++; } return ans; } // Driver Code public static void Main() { int[] arr = { 4, 3, 2, 2, 1, 6 }; Console.WriteLine(find(arr, 6)); } } // This code is contributed by ukasp. JavaScript <script> // JavaScript code for the above approach // Function to find the total number // of operations required function find(arr, size) { let sum = 0; // Find sum of all element of array arr. for (let i = 0; i < size; i++) { sum += arr[i]; } // Variable to store integer which needs // to be subtracted from arr in total. let diff = sum - ((2 * size) - 1); let ans = 0; let i = size - 1; arr.sort(function (a, b) { return a - b }) // Iteration to calcute total number of // subtraction required to get desired array. while (diff > 0) { diff -= (arr[i] - 1); i--; ans++; } return ans; } // Driver Code let arr = [4, 3, 2, 2, 1, 6]; document.write(find(arr, 6) + '<br>'); // This code is contributed by Potta Lokesh </script> Output: 2 Time Complexity: O(n*log(n))Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimize Array elements to be reduced to make subsequences sum 1 to Array max possible S saurabh15899 Follow Improve Article Tags : Greedy Sorting Mathematical DSA Arrays subsequence +2 More Practice Tags : ArraysGreedyMathematicalSorting Similar Reads Minimum steps required to reduce all array elements to 1 based on given steps Given an array arr[] of size N. The task is to find the minimum steps required to reduce all array elements to 1. In each step, perform the following given operation: Choose any starting index, say i, and jump to the (arr[i] + i)th index, reducing ith as well as (arr[i] + i)th index by 1, follow thi 8 min read Minimize operations to reduce Array sum by half by reducing any elements by half Given an array Arr[], the task is to find out the minimum number of operations to make the sum of array elements lesser or equal to half of its initial value. In one such operation, it is allowed to half the value of any array element. Examples: Input: Arr[] = [4, 6, 3, 9, 10, 2]Output: 5Explanation 5 min read Minimize steps to make Array elements 0 by reducing same A[i] - X from Subarray Given an array A[] of size N, the task is to find the minimum number of operations required to make all the elements of the array zero. In one step, the following operations are done on a subarray of the given array: Any integer X is chosenIf an element is greater than X (A[i] > X), the array ele 6 min read Minimize subarray increments/decrements required to reduce all array elements to 0 Given an array arr[], select any subarray and apply any one of the below operations on each element of the subarray: Increment by oneDecrement by one The task is to print the minimum number of above-mentioned increment/decrement operations required to reduce all array elements to 0. Examples: Input: 5 min read Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2 Given an array A[] consisting of N distinct integers and another array B[] consisting of M integers, the task is to find the minimum number of elements to be added to the array B[] such that the array A[] becomes the subsequence of the array B[]. Examples: Input: N = 5, M = 6, A[] = {1, 2, 3, 4, 5}, 9 min read Minimize shifting half of each element to either sides to reduce Array Given an array arr[] of size N, the task is to find minimum number of steps required to reduce all Array elements to 0 except the first and last, where in each step: Choose any index i (0<i<N-1) and decrement the element at that index (arr[i]) by 2Then increment any two elements, arr[j], where 8 min read Minimize elements to be added to a given array such that it contains another given array as its subsequence Given an array A[] consisting of N distinct integers and another array B[] consisting of M integers, the task is to find the minimum number of elements to be added to the array B[] such that the array A[] becomes the subsequence of the array B[]. Examples: Input: N = 5, M = 6, A[] = {1, 2, 3, 4, 5}, 15+ min read Rearrange array elements to maximize the sum of MEX of all prefix arrays Given an array arr[] of size N, the task is to rearrange the array elements such that the sum of MEX of all prefix arrays is the maximum possible. Note: MEX of a sequence is the minimum non-negative number not present in the sequence. Examples: Input: arr[] = {2, 0, 1}Output: 0, 1, 2Explanation:Sum 7 min read Minimum Decrements on Subarrays required to reduce all Array elements to zero Given an array arr[] consisting of N non-negative integers, the task is to find the minimum number of subarrays that needs to be reduced by 1 such that all the array elements are equal to 0. Example: Input: arr[] = {1, 2, 3, 2, 1}Output: 3Explanation: Operation 1: {1, 2, 3, 2, 1} -> {0, 1, 2, 1, 5 min read Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] = 7 min read Like