import java.util.*; class GFG { public static void main(String[] args) { int[][] interval = { { 1, 3, 2 }, { 4, 5, 2 }, { 1, 5, 5 } }; int maxValue = maxTwoNonOverLapping(interval); System.out.println(maxValue); } public static int maxTwoNonOverLapping( int[][] interval) { Arrays.sort(interval, (a, b) -> a[0] – b[0]); PriorityQueue pq = new PriorityQueue( (a, b) -> a[0] – b[0]); int max = 0; int ans = 0; for (int[] e : interval) { while (!pq.isEmpty()) […]

Continue ReadingGiven 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 […]

Continue ReadingGiven 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 […]

Continue Readingusing System;using System.Collections.Generic;public class GFG { public class Geeks { public void insert(Queue q, int k) { q.Enqueue(k); } public int findFrequency(Queue q, int k) { int cntFrequency = 0; int size = q.Count; while (size– != 0) { int x = q.Peek(); q.Dequeue(); if (x == k) { cntFrequency++; } q.Enqueue(x); } return cntFrequency; }} public static void Main() { Queue q = new Queue(); int N = 8; int[] a = new int[] { 1, 2, 3, […]

Continue Reading#include using namespace std; string changeNtoM(int N, int M){ if (N == M) { return ” “; } if (M == 0) { return “-“; } queue q; unordered_map visited; q.push({ N, “” }), visited[N] = 1; q.push({ 1, “/” }), visited[1] = 1; while (!q.empty()) { pair cur = q.front(); q.pop(); if (cur.first == M) { return cur.second; } if (!visited[cur.first + cur.first] && cur.first + cur.first

Continue ReadingGiven an undirected connected graph of N nodes and M edges. Each node has a light but at a time it can be either green or red. Initially, all the node is of green color. After every T seconds, the color of light changes from green to red and vice-versa. It is possible to travel […]

Continue Reading#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[0].size(); vector visited(M, vector(N, false)); priority_queue pq; for (int i = 0; i […]

Continue ReadingReduce the given Array of [1, N] by rotating left or right based on given conditionsGiven a sorted array arr[] of the first N Natural Numbers and an integer X, the task is to print the last remaining element after performing the below operations (N – 1) times:Examples:Input: N = 5, arr[] = {1, 2, […]

Continue Reading#include using namespace std; vector generateBinaryTree(int n){ vector options = { 0, 1, 2 }; map mp; queue q; q.push(1); vector v; int count = 1; mp[1] = 1; while (!q.empty() or count < n) { int front; if(!q.empty()) { front = q.front(); q.pop(); } int numberOfChilds = options[rand() % (options.size())]; if (count >= n) continue; while (numberOfChilds–) { int child = rand() % n + 1; while (mp[child]) { child++; if (child > n) { child = 1; } } count++; mp[child] […]

Continue Reading#include using namespace std;class Node {public: int val; Node *left, *right;};Node* newNode(int item){ Node* temp = new Node(); temp->val = item; temp->left = temp->right = NULL; return temp;}int sum = 0;int rangeSumBST(Node* root, int low, int high){ if (root == NULL) return 0; queue q; q.push(root); while (q.empty() == false) { Node* curr = q.front(); q.pop(); if (curr->val >= low && curr->val val; } if (curr->left != NULL && curr->val > low) q.push(curr->left); if (curr->right != […]

Continue Reading