Category: Sorting

Maximum sum of at most two non-overlapping intervals in a list of Intervals | Interval Scheduling Problem

  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 Reading

Minimum number of deques required to make the array sorted

#include using namespace std;int minDeques(vector arr){            vector fronts, backs;        for (int i = 0; i < arr.size(); i++) {                        bool hasBeenPushed = false;        for (int j = 0; j < fronts.size(); j++) {                                    if (arr[i] < fronts[j]) {                bool isSafe = true;                for (int k = 0; k < arr.size(); k++) {                    if (arr[i] < arr[k]                        && arr[k] < fronts[j]) {                        isSafe = false;                        break;                    }                }                                                if (isSafe) […]

Continue Reading

Find the missing value from Array B formed by adding some value X to Array A

def findHiddenMissing(arr1, arr2, N):            arr1.sort()    arr2.sort()        mp = {}            for i in range(0, N – 1):                                                               a = arr2[i] – arr1[i]                                b = arr2[i] – arr1[i + 1]                if (a != b):                                    if (a > 0):                if not a in mp:                    mp[a] = 1                else:                    mp[a] += 1                                if (b > 0):                if not b in mp:                    mp[b] = 1                else:                    mp[b] += 1                        else:                                    if (a > 0):                if not a in mp:                    mp[a] = […]

Continue Reading

Sort an array of strings based on the given order | Set-2

Given an array of strings words[] and the sequential order of alphabets, the task is to sort the array according to the given order. Assume that the dictionary and the words only contain lowercase English alphabets.Examples: Input: words[] = {“hello”, “geeksforgeeks”}, order[] = “hlabcdefgijkmnopqrstuvwxyz”Output: hello geeksforgeeks Explanation: According to the given order ‘h’ occurs before ‘g’ and hence […]

Continue Reading