Sunday, 28 Nov 2021
Category: Heap

Given an array arr[] of size N, the task is to find the minimum number of operations required to make all array elements zero. In one operation, select a pair of elements and subtract the smaller element from both elements in the array.Example:Input: arr[] = {1, 2, 3, 4}Output: 3Explanation: Pick the elements in the […]

Given an array arr[] of size N and an integer X, the task is to find the length of the longest subsequence such that the prefix sum at every element of the subsequence remains greater than zero.Example:Input: arr[] = {-2, -1, 1, 2, -2}, N = 5Output: 3Explanation: The sequence can be made of elements […]

#include using namespace std;  vector dir    = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };  struct node {      int height;    int x, y;};  struct Compare {          bool operator()(node const& a, node const& b)    {        return a.height > b.height;    }};  int trapRainWater(vector& heightMap){    int M = heightMap.size();    int N = heightMap.size();              vector visited(M,                                  vector(N, false));          priority_queue pq;          for (int i = 0; i […]

Find min and max values among all maximum leaf nodes from all possible Binary Max HeapGiven a positive integer N, the task is to find the largest and smallest elements, from the maximum leaf nodes of every possible binary max-heap formed by taking the first N natural numbers as the nodes’ value of the binary […]

#include using namespace std;  double change(int pass, int total){    double currentPassRatio, newPassRatio;    double increase;          currentPassRatio = ((double)pass)                       / total;          newPassRatio = ((double)(pass + 1))                   / (total + 1);          increase = newPassRatio               – currentPassRatio;          return increase;}  double maximumAverage(    vector v, int M,    int N){        double sum = 0;      double increase, average;              priority_queue pq;    for (int i = 0; i < N; i++) {                          increase = change(v[i], v[i]);                          pq.push({ increase, i });                  average = v[i] * 1.0 […]

import java.util.*;import java.lang.*;  class GFG {              static int minTime(int[] A, int n, int K)    {                int max_ability = A;                  for (int i = 1; i < n; i++) {            max_ability = Math.max(                max_ability, A[i]);        }                  int tmp[] = new int[max_ability + 1];                          int count = 0;                  for (int i = 0; i < n; i++) {            tmp[A[i]]++;        }                  for (int i = max_ability;             i >= 0; i–) {              if (tmp[i] != […]

import java.util.*;    class Node {        int val;     Node next;             Node(int val)     {         this.val = val;         this.next = null;     } }    class GFG {             public static void mergeSheets(         Node lists[])     {                  PriorityQueue pq                = new PriorityQueue(                 new Comparator() {                                                                  public int compare(Node a, […]