Minimize Array elements to be reduced to make subsequences sum 1 to Array max possible

• Last Updated : 21 Jan, 2022

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: 2
Explanation: 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 using namespace std; // Function to find the total number// of operations requiredint 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 Codeint main(){    int arr[] = { 4, 3, 2, 2, 1, 6 };     cout << find(arr, 6) << "\n";     return 0;}

Java

 // Java program for the above approachimport 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

C#

 // C# program for the above approachusing 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



Output:
2

Time Complexity: O(n*log(n))
Auxiliary Space: O(n)

My Personal Notes arrow_drop_up