Minimize Array elements to be reduced to make subsequences sum 1 to Array max possible
Last Updated :
25 May, 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++
#include <bits/stdc++.h>
using namespace std;
int find( int arr[], int size)
{
int sum = 0;
for ( int i = 0; i < size; i++) {
sum += arr[i];
}
int diff = sum - ((2 * size) - 1);
int ans = 0;
int i = size - 1;
sort(arr, arr + size);
while (diff > 0) {
diff -= (arr[i] - 1);
i--;
ans++;
}
return ans;
}
int main()
{
int arr[] = { 4, 3, 2, 2, 1, 6 };
cout << find(arr, 6) << "\n" ;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int find( int arr[], int size)
{
int sum = 0 ;
for ( int i = 0 ; i < size; i++) {
sum += arr[i];
}
int diff = sum - (( 2 * size) - 1 );
int ans = 0 ;
int i = size - 1 ;
Arrays.sort(arr);
while (diff > 0 ) {
diff -= (arr[i] - 1 );
i--;
ans++;
}
return ans;
}
public static void main (String[] args) {
int arr[] = { 4 , 3 , 2 , 2 , 1 , 6 };
int N = arr.length;
System.out.println( find(arr, N));
}
}
|
Python
def find(arr, size):
sum = 0
for i in range ( 0 , size):
sum = sum + arr[i]
diff = sum - (( 2 * size) - 1 )
ans = 0
i = size - 1
arr.sort()
while (diff > 0 ):
diff = diff - (arr[i] - 1 )
i = i - 1
ans = ans + 1
return ans
arr = [ 4 , 3 , 2 , 2 , 1 , 6 ]
print (find(arr, 6 ))
|
C#
using System;
class GFG {
static int find( int [] arr, int size)
{
int sum = 0;
for ( int i = 0; i < size; i++) {
sum += arr[i];
}
int diff = sum - ((2 * size) - 1);
int ans = 0;
int j = size - 1;
Array.Sort(arr);
while (diff > 0) {
diff -= (arr[j] - 1);
j--;
ans++;
}
return ans;
}
public static void Main()
{
int [] arr = { 4, 3, 2, 2, 1, 6 };
Console.WriteLine(find(arr, 6));
}
}
|
Javascript
<script>
function find(arr, size) {
let sum = 0;
for (let i = 0; i < size; i++) {
sum += arr[i];
}
let diff = sum - ((2 * size) - 1);
let ans = 0;
let i = size - 1;
arr.sort( function (a, b) { return a - b })
while (diff > 0) {
diff -= (arr[i] - 1);
i--;
ans++;
}
return ans;
}
let arr = [4, 3, 2, 2, 1, 6];
document.write(find(arr, 6) + '<br>' );
</script>
|
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...