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[] consisting of permutation of the first N natural numbers, the task is to find the expected number of swaps to sort the given array where the probability of swapping any inversion pair is equal and the probability of swapping any non-inversion pair is 0.Examples:Attention reader! Don’t stop learning now. Get hold […]

Continue ReadingGiven two integers N and K, the task is to find the count of binary strings of at most N length that can be formed such that the count of consecutive 1‘s is always a multiple of K.Example:Input: N = 3, K = 2Output: 6Explanation: Binary strings of atmost N length containing consecutive 1’s as […]

Continue Reading#include #define SIZE 100005using namespace std; void preprocess(string& s, string& t, int prefix[][SIZE], int n, int i){ prefix[i][0] = (s[0] != t[0]); for (int j = 1; j < n; j++) { prefix[i][j] = prefix[i][j - 1] + (s[j] != t[j % 3]); } return;} void minChangesNonPalindrome( string str, int N, int Q, vector queries){ int prefix[6][SIZE]; vector sequences = { "012", "021", "102", "120", "201", "210" }; for (int i = 0; […]

Continue Reading#include using namespace std; #define M 100000 vector tree(4 * M + 1); void update_tree(int start, int end, int update_idx, int length_t, int count_c, int idx){ if (start == end && start == update_idx) { tree[idx].first = max(tree[idx].first, length_t); tree[idx].second = count_c; return; } if (update_idx < start || end < update_idx) { return; } int mid = (start + end) / 2; update_tree(start, mid, update_idx, length_t, count_c, 2 * idx); update_tree(mid + 1, end, update_idx, length_t, […]

Continue ReadingGiven an array arr[] of N integers, the task is to find the maximum count of K, i.e, consecutive integers from 0 to K, that is present in the set S, where S contains all the possible subset-sum values of the array arr[].Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA […]

Continue Reading#include using namespace std; int totalValidArrays(int a[], int b[], int N){ int dp[N + 1][b[N – 1] + 1]; int pref[N + 1][b[N – 1] + 1]; memset(dp, 0, sizeof(dp)), memset(pref, 0, sizeof(pref)); dp[0][0] = 1; for (int i = 0; i

Continue ReadingGiven two positive integers L and R and a binary string S of size N, the task is to check if the end of the string is reached from the index 0 by a series of jumps of indices, say i such that S[i] is 0 jumps are allowed in the range [L, R]. If […]

Continue Reading#include using namespace std;#define ar array ar expand(int idx, vector& A){ int l = idx – 1; int r = idx + 1; while (l >= 0) { if ((int)(A[idx]) > A[l]) { –l; } else { break; } } if (l < 0 || l == idx) { l = -2; } while (r < (int)(A.size())) { if ((int)A[idx] > A[r]) { ++r; } else { break; } } if (r >= (int)(A.size()) || r == idx) […]

Continue Readingconst INT_MAX = 2147483647; const minZeroeCount = (S, K, Q) => { let N = S.length; if (K == 0 || Q == 0) { let cnt0 = 0; for (let i = 0; i < N; ++i) { if (S[i] == '0') cnt0++; } return cnt0; } if (Q >= Math.ceil(N / K)) { return 0; } const dp = new Array(N + 1).fill(INT_MAX).map(() => new Array(Q […]

Continue Reading