📌  相关文章
📜  通过使用任何正值成对减少元素,将给定数组转换为 0

📅  最后修改于: 2022-05-13 01:57:48.366000             🧑  作者: Mango

通过使用任何正值成对减少元素,将给定数组转换为 0

给定一个大小为N的数组arr[] ,任务是通过将数组元素的值成对递减任何正值来找到将数组元素转换为零的操作数。如果无法将数组元素转换为 0,则返回-1

方法: 该任务可以通过将数组的所有元素存储在优先级队列中来解决,然后我们需要从队列中选择两个最大元素的对,然后从它们中减去 1 ,直到只剩下一个或没有正元素。
请按照以下步骤解决问题:

  • 将元素存储在优先级队列中
  • 进行一次while循环,直到优先级队列的大小大于或等于2,并且在每次迭代中:-
    1. 弹出前两个元素并将它们存储在变量ele1ele2 中
    2. 将 ele1 和 ele2 减 1,如果其中任何一个仍然大于零,则将它们再次推入队列。
  • 如果队列为空,则打印所需的操作数,否则-1

下面是上述算法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check whether
// all element of vector can
// become zero after the operations
void gfg(vector& v)
{
 
    // Priroty queue to store
    // elements of vector v
    priority_queue q;
 
    // Loop to store elements
    // in priroty queue
    for (auto x : v) {
        q.push(x);
    }
 
    // Stores the number
    // of operations needed
    int cnt = 0;
    while (q.size() >= 2) {
 
        // Variable to store greatest
        // element of priority queue
        int ele1 = q.top();
        q.pop();
 
        // Variable to store second greatest
        // element of priority queue
        int ele2 = q.top();
        q.pop();
 
        // Decrementing both by 1
        ele1--;
        ele2--;
        cnt += 2;
 
        // If elements are greater
        // then zero it is again
        // stored in the priority queue
        if (ele1) {
            q.push(ele1);
        }
        if (ele2) {
            q.push(ele2);
        }
    }
 
    if (q.size() == 0)
        cout << cnt << endl;
    else
        cout << -1;
}
 
// Driver code
int main()
{
 
    vector v = { 5, 3, 4 };
    gfg(v);
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
  // Function to check whether
  // all element of vector can
  // become zero after the operations
  static void gfg(int[] v)
  {
 
    // Priroty queue to store
    // elements of vector v
    PriorityQueue q = new PriorityQueue<>(Collections.reverseOrder());
 
    // Loop to store elements
    // in priroty queue
    for (int x : v) {
      q.add(x);
    }
 
    // Stores the number
    // of operations needed
    int cnt = 0;
    while (q.size() >= 2) {
 
      // Variable to store greatest
      // element of priority queue
      int ele1 = q.peek();
      q.remove();
 
      // Variable to store second greatest
      // element of priority queue
      int ele2 = q.peek();
      q.remove();
 
      // Decrementing both by 1
      ele1--;
      ele2--;
      cnt += 2;
 
      // If elements are greater
      // then zero it is again
      // stored in the priority queue
      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);
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    int[] v = { 5, 3, 4 };
    gfg(v);
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python code for the above approach
from queue import PriorityQueue
 
# Function to check whether
# all element of vector can
# become zero after the operations
def gfg(v):
 
    # Priroty queue to store
    # elements of vector v
    q = PriorityQueue()
 
    # Loop to store elements
    # in priroty queue
    for i in range(len(v)):
        q.put(-1 * v[i])
 
    # Stores the number
    # of operations needed
    cnt = 0
    while (q.qsize() >= 2):
 
        # Variable to store greatest
        # element of priority queue
        ele1 = -1 * q.get()
 
        # Variable to store second greatest
        # element of priority queue
        ele2 = -1 * q.get()
        # Decrementing both by 1
        ele1 = ele1-1
        ele2 = ele2-1
        cnt = cnt + 2
 
        # If elements are greater
        # then zero it is again
        # stored in the priority queue
        if ele1 > 0:
            q.put(-1 * ele1)
        if ele2 > 0:
            q.put(-1 * ele2)
 
    if q.qsize() == 0:
        print(cnt)
    else:
        print(-1)
 
# Driver code
v = [5, 3, 4]
gfg(v)
 
# This code is contributed by Potta Lokesh


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Function to check whether
  // all element of vector can
  // become zero after the operations
  static void gfg(int[] v)
  {
 
    // Priroty queue to store
    // elements of vector v
    List q = new List();
 
    // Loop to store elements
    // in priroty queue
    foreach(int x in v) {
      q.Add(x);
    }
 
    // Stores the number
    // of operations needed
    int cnt = 0;
    while (q.Count >= 2) {
 
      // Variable to store greatest
      // element of priority queue
      int ele1 = q[0];
      q.RemoveAt(0);
 
      // Variable to store second greatest
      // element of priority queue
      int ele2 = q[0];
      q.RemoveAt(0);
 
      // Decrementing both by 1
      ele1--;
      ele2--;
      cnt += 2;
 
      // If elements are greater
      // then zero it is again
      // stored in the priority queue
      if (ele1 > 0) {
        q.Add(ele1);
      }
      if (ele2 > 0) {
        q.Add(ele2);
      }
    }
 
    if (q.Count == 0)
      Console.Write(cnt +"\n");
    else
      Console.Write(-1);
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    int[] v = { 5, 3, 4 };
    gfg(v);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
12

时间复杂度 在)
辅助空间 O(N)