# 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++

```// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to find the minimum value of X
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;
}

// Driver code
int main()
{
int A[] = { 7, 18, 16, 14 };
int N = sizeof(A) / sizeof(A[0]);

// Function Call
cout << minCapacity(N, A);

return 0;
}```

## Java

```import java.util.Arrays;
public class GFG
{

// Function to find the minimum value of X
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;

// Function Call
System.out.println(minCapacity(N, A));
}
}

// This code is contributed by Pushpesh Raj.```

## Python3

```# Python3 code to implement the approach

# Function to find the minimum value of X
def minCapacity(n, a) :

a.sort()
ans = 0;
for i in range(n) :
ans = max(ans, a[i] - i);

return ans;

# Driver code
if __name__ == "__main__":

A = [ 7, 18, 16, 14 ];
N = len(A);

# Function Call
print(minCapacity(N, A));

# This code is contributed by AnkThon```

## C#

```using System;
using System.Collections.Generic;

public class GFG {

// Function to find the minimum value of X
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;

// Function Call
Console.WriteLine(minCapacity(N, A));
}
}
// This code is contributed by akashish__```

## Javascript

```        // JavaScript code for the above approach

// Function to find the minimum value of X
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;
}

// Driver code
let A = [7, 18, 16, 14];
let N = A.length;

// Function Call
console.log(minCapacity(N, A));

// This code is contributed by Potta Lokesh
```
Output

`15`

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

My Personal Notes arrow_drop_up