Convert given Array to 0 by reducing elements pairwise with any positive value
Last Updated :
02 Mar, 2022
Given an array arr[] of size N, the task is to find the number of operations to convert array elements to zero by decrementing the value of array elements in pairs by any positive value. If the array elements can’t be converted to 0, return -1.
Input: arr[] = {3, 2}
Output: -1
Explanation: All the array elements can’t be converted to 0
Input: arr[] = {5, 4, 3}
Output: 12
Explanation: Subtract 1 from pair (4, 3) we get {5, 3, 2}, subtract 3 from (5, 3) we get {2, 0, 2}, Subtract 2 from pair (2, 2) we get {0, 0, 0}
Approach: The task can be solved by storing all elements of an array in a priority queue, Then we need to choose pair of the two greatest elements from the queue and subtract 1 from them until only one or no positive element is left.
Follow the below steps to solve the problem:
- Store elements in the priority queue
- Take a while loop until the size of the priority queue is greater than or equal to 2, and in each iteration:-
- Pop the first two elements and store them in variables ele1 and ele2
- Decrement ele1 and ele2 with 1, If any of them are still greater than zero push them again in the queue.
- If the queue is empty print the number of operations needed, else -1
Below is the implementation of the above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
void gfg(vector< int >& v)
{
priority_queue< int > q;
for ( auto x : v) {
q.push(x);
}
int cnt = 0;
while (q.size() >= 2) {
int ele1 = q.top();
q.pop();
int ele2 = q.top();
q.pop();
ele1--;
ele2--;
cnt += 2;
if (ele1) {
q.push(ele1);
}
if (ele2) {
q.push(ele2);
}
}
if (q.size() == 0)
cout << cnt << endl;
else
cout << -1;
}
int main()
{
vector< int > v = { 5, 3, 4 };
gfg(v);
}
|
Java
import java.util.*;
class GFG{
static void gfg( int [] v)
{
PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
for ( int x : v) {
q.add(x);
}
int cnt = 0 ;
while (q.size() >= 2 ) {
int ele1 = q.peek();
q.remove();
int ele2 = q.peek();
q.remove();
ele1--;
ele2--;
cnt += 2 ;
if (ele1> 0 ) {
q.add(ele1);
}
if (ele2> 0 ) {
q.add(ele2);
}
}
if (q.size() == 0 )
System.out.print(cnt + "\n" );
else
System.out.print(- 1 );
}
public static void main(String[] args)
{
int [] v = { 5 , 3 , 4 };
gfg(v);
}
}
|
Python3
from queue import PriorityQueue
def gfg(v):
q = PriorityQueue()
for i in range ( len (v)):
q.put( - 1 * v[i])
cnt = 0
while (q.qsize() > = 2 ):
ele1 = - 1 * q.get()
ele2 = - 1 * q.get()
ele1 = ele1 - 1
ele2 = ele2 - 1
cnt = cnt + 2
if ele1 > 0 :
q.put( - 1 * ele1)
if ele2 > 0 :
q.put( - 1 * ele2)
if q.qsize() = = 0 :
print (cnt)
else :
print ( - 1 )
v = [ 5 , 3 , 4 ]
gfg(v)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void gfg( int [] v)
{
List< int > q = new List< int >();
foreach ( int x in v) {
q.Add(x);
}
int cnt = 0;
while (q.Count >= 2) {
int ele1 = q[0];
q.RemoveAt(0);
int ele2 = q[0];
q.RemoveAt(0);
ele1--;
ele2--;
cnt += 2;
if (ele1 > 0) {
q.Add(ele1);
}
if (ele2 > 0) {
q.Add(ele2);
}
}
if (q.Count == 0)
Console.Write(cnt + "\n" );
else
Console.Write(-1);
}
public static void Main(String[] args)
{
int [] v = { 5, 3, 4 };
gfg(v);
}
}
|
Javascript
<script>
function gfg(v) {
q = [];
for (x of v) {
q.push(x);
}
q.sort((a, b)=>b - a);
var cnt = 0;
while (q.length >= 2) {
var ele1 = q[0];
q.splice(0, 1);
var ele2 = q[0];
q.splice(0, 1);
ele1 -= 1;
ele2 -=1;
cnt += 2;
if (ele1 > 0) {
q.push(ele1);
}
if (ele2 > 0) {
q.push(ele2);
}
}
if (q.length == 0)
document.write(cnt + "\n" );
else
document.write(-1);
}
var v = [ 5, 3, 4 ];
gfg(v);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...