#include using namespace std; int maxLength(string str, int n, char c, int k){ int ans = -1; int cnt = 0; int left = 0; for (int right = 0; right < n; right++) { if (str[right] == c) { cnt++; } while (cnt > k) { if (str[left] == c) { cnt–; } left++; } ans = max(ans, right – left + 1); } return ans;} int maxConsecutiveSegment(string S, int K){ int N = […]

Continue Reading#include using namespace std; void findMostFrequent(int arr[], int N, int K){ int start = 0, end = 0; sort(arr, arr + N); int mostFreq = INT_MIN, sum = 0; for (end = 0; end < N; end++) { sum = sum + arr[end]; while (sum + K < arr[end] * (end - start + 1)) { sum = sum - arr[start]; start++; } mostFreq = max(mostFreq, end - […]

Continue Readingimport java.io.*;import java.lang.*;import java.util.*;public class GFG { static boolean isPrime(int n) { if (n

Continue ReadingCount number of substrings having at least K distinct characters Given a string S consisting of N characters and a positive integer K, the task is to count the number of substrings having at least K distinct characters.Examples:Input: S = “abcca”, K = 3Output: 4Explanation:The substrings that contain at least K(= 3) distinct characters are:“abc”: […]

Continue ReadingMinimize length of a string by removing suffixes and prefixes of same charactersGiven a string S of length N consisting only of characters ‘a’, ‘b’, and ‘c’, the task is to minimize the length of the given string by performing the following operations only once:Divide the string into two non-empty substrings and then, append the […]

Continue ReadingGiven an array arr[] consisting of N integers, the task is to check if the sequence of numbers formed by repeatedly removing the middle elements from the given array arr[] is sorted or not. If there are two middle elements, then remove any one of them.Examples:Input: arr[] = {4, 3, 1, 2, 5}Output: YesExplanation:The order […]

Continue ReadingGiven a binary string S and an array A[], both of size N, the task is to find the maximum score possible by removing substrings of any length, say K, consisting of the same characters, and adding A[K] to the score. Examples: Input: S = “abb”, A = [1, 3, 1]Output: 4Explanation: Initially, score = 0 […]

Continue ReadingMinimum number of array elements from either ends required to be subtracted from X to reduce X to 0 Given an array nums[] and an integer X, the task is to reduce X to 0 by removing either the leftmost or the rightmost array elements and subtracting its value from X, minimum number of times. […]

Continue Readingimport java.util.*; class GFG { public static int makeSumEqual( int[] a, int[] b, int K, int M, int N) { int sum1 = 0; int sum2 = 0; for (int el : a) sum1 += el; for (int el : b) sum2 += […]

Continue Reading