Minimize X such that array can made 0 by reducing elements less than X
Last Updated :
20 Dec, 2022
Given an array A[] of size N. In each step reduce an element to 0 and decrement all the non-zero elements by 1 till all the elements become 0. Find the minimum possible value of X such that the array can be made 0 by deleting values less than or equal to X in each step.
Examples:
Input: N = 4, A = {7, 18, 16, 14}
Output: 15
Explanation: At the beginning of the operation, the array has {7, 18, 16, 14}
Make 1st element 0, remaining array elements {0, 17, 15, 13}
Make 3rd element 0, remaining array elements {0, 16, 0, 12}
Make 4th element 0, remaining array elements {0, 15, 0, 0}
Make 2nd element 0, remaining array elements {0, 0, 0, 0}
Therefore the minimum value of X needs to be 15 units.
Input: N = 1, A = {3}
Output: 3
Explanation: X has to be 3
Approach: This problem can be solved using the Greedy Algorithm.
Make the smallest element 0 first so that all the greater elements are reduced by 1 so that X can be minimized.
Follow the steps mentioned below to implement the idea:
- Sort the given array.
- Start making elements 0 starting from the smallest element.
- Hence, for every ith array element the value at that time would have decreased by i units i.e. value = A[i] – i.
- Find the maximum value among A[i] – i to ensure all elements become 0.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minCapacity( int n, int a[])
{
sort(a, a + n);
int ans = 0;
for ( int i = 0; i < n; i++)
ans = max(ans, a[i] - i);
return ans;
}
int main()
{
int A[] = { 7, 18, 16, 14 };
int N = sizeof (A) / sizeof (A[0]);
cout << minCapacity(N, A);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG
{
static int minCapacity( int n, int a[])
{
Arrays.sort(a);
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
ans = Math.max(ans, a[i] - i);
return ans;
}
public static void main(String[] args)
{
int A[] = { 7 , 18 , 16 , 14 };
int N=A.length;
System.out.println(minCapacity(N, A));
}
}
|
Python3
def minCapacity(n, a) :
a.sort()
ans = 0 ;
for i in range (n) :
ans = max (ans, a[i] - i);
return ans;
if __name__ = = "__main__" :
A = [ 7 , 18 , 16 , 14 ];
N = len (A);
print (minCapacity(N, A));
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int minCapacity( int n, int [] a)
{
Array.Sort(a);
int ans = 0;
for ( int i = 0; i < n; i++)
ans = Math.Max(ans, a[i] - i);
return ans;
}
static public void Main()
{
int [] A = { 7, 18, 16, 14 };
int N = A.Length;
Console.WriteLine(minCapacity(N, A));
}
}
|
Javascript
function minCapacity(n, a) {
a.sort( function (a, b) { return a - b })
let ans = 0;
for (let i = 0; i < n; i++)
ans = Math.max(ans, a[i] - i);
return ans;
}
let A = [7, 18, 16, 14];
let N = A.length;
console.log(minCapacity(N, A));
|
Time Complexity: O(N * log N) // the inbuilt sort function takes O(n *log n) time to process the elements
Auxiliary Space: O(1) // since no extra array is used hence space occupied is constant
Share your thoughts in the comments
Please Login to comment...