# Minimal operations to make Array parts 0 by decrementing pair or single ingredient

0
6 Given an array arr[] of dimension N, the duty is to seek out the minimal variety of operations required to scale back all three parts of the array to zero. Following operations are allowed:

• Cut back 2 totally different array parts by one.
• Cut back a single array ingredient by one.

#### Instance:

Enter: arr[] = {1, 2, 3}, N = 3
Output: 3
Clarification : Operation 1: scale back 3 and a couple of to get {1, 1, 2}
Operation 2: reduuce 1 and a couple of to get {1, 0, 1}
Operation 3: scale back each 1s to get {0, 0, 0}

Enter: arr[] = {5, 1, 2, 9, 8}, N = 5
Output: 13

#### Method:

This downside will be solved utilizing grasping method. The concept is to scale back the two largest parts at a time or (if not potential) 1 at a time.  As we’d like the most important parts in every step, we are able to use a max heap.

The next steps will be taken to unravel this method:

• Provoke a rely variable as 0.
• Insert all the weather in a max heap.
• Cut back the 2 largest parts.
• Insert the decreased values once more into the heap.
• Repeat above talked about steps till all array parts turn out to be zero and improve the rely at every iteration.
• Cease when all array parts are zero.

Beneath is the implementation of the above method:

## Java

 `import` `java.util.*;` ` `  `class` `GFG {` ` `  `    ``public` `static` `int` `reduceArray(``int` `arr[], ``int` `N)` `    ``{` ` `  `        ``int` `rely = ``0``;` `        ``PriorityQueue pq = ``new` `PriorityQueue<>();` ` `  `        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``pq.add(arr[i] * -``1``);` `        ``}` `        ``whereas` `(pq.dimension() > ``1``) {` `            ``int` `temp1 = pq.ballot();` `            ``int` `temp2 = pq.ballot();` `            ``rely++;` `            ``temp1++;` `            ``temp2++;` `            ``if` `(temp1 != ``0``)` `                ``pq.add(temp1);` `            ``if` `(temp2 != ``0``)` `                ``pq.add(temp2);` `        ``}` `        ``if` `(pq.dimension() > ``0``)` `            ``rely -= pq.ballot();` `        ``return` `rely;` `    ``}` ` `  `    ` `    ``public` `static` `void` `most important(String[] args)` `    ``{` `        ``int` `arr[] = { ``1``, ``2``, ``3` `};` `        ``int` `N = ``3``;` `        ``int` `rely = reduceArray(arr, N);` `        ``System.out.println(rely);` `    ``}` `}`

Time Complexity: O(N * logN)
Auxiliary House: O(N)