Saturday, 4 Dec 2021
Category: Pattern Searching

#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 = […]

Given an array, arr[] and a positive integer K. The task is to find the position say i of the element in arr[] such that prefix sum till i-1, i and suffix sum till i+1 are in Geometric Progression with common ratio K. Examples:Input: arr[] = { 5, 1, 4, 20, 6, 15, 9, 10 }, […]

Given an array arr[] of size N, where arr[i] is natural numbers less than or equal to N, the task is to find all the numbers in the range [1, N] that are not present in the given array.Examples:Input: arr[ ] = {5, 5, 4, 4, 2}Output: 1 3Explanation: For all numbers in the range [1, […]

#include using namespace std;  int minimumPalindromicStrings(string S){        int N = S.length();                      vector freq(26);    for (int i = 0; i < N; i++) {        freq[S[i] - 'a']++;    }          vector oddFreqCharacters;                                for (int i = 0; i < 26; i++) {          if (freq[i] & 1) {              oddFreqCharacters.push_back(i);            freq[i]--;        }          freq[i] /= 2;    }          vector ans;              if (oddFreqCharacters.empty()) {                          string left = "";          for (int i = 0; i < 26; i++) {            for (int j = […]

Given three integers, C0, C1 and C2 frequencies of 0s, 1s and 2s in a group S. The task is to find the maximum number of groups having the sum divisible by 3, the condition is the sum(S) is divisible by 3 and the union of all groups must be equal to SExamples:Input: C0 = […]

Given a binary string str[] of size N and an integer M. This binary string can be modified by flipping all the 0’s to 1 which have exactly one 1 as a neighbour. The task is to find the final state of the binary string after M such iterations.Note: 2≤N≤103, 1≤M≤109Examples:Input: str=”01100″, M=1Output: 11110Explanation: After […]

Count characters of a string which when removed individually makes the string equal to another stringGiven two strings A and B of size N and M respectively, the task is to count characters of the string A, which when removed individually makes both the strings equal. If there exists several such characters, then print their […]

import java.io.*;import java.util.*;  public class Main {                  static void minimumProdArray(long a[],                                 long b[], int l)    {        long total = 0;                  for (int i = 0; i < a.length; ++i) {            total += a[i] * b[i];        }          long min = Integer.MAX_VALUE;          int first = 0;        int second = 0;                  for (int i = 0; i < a.length; ++i) {              int left = i - 1;            int right = i + […]

import java.io.*;import java.util.*;  class GFG {                  static int count(String S, int N)    {                  char ch[] = { ‘u’, ‘o’, ‘i’, ‘e’, ‘a’ };                  int j = 0;                  int res = 0;                          int count = 0;                  HashSet mp = new HashSet();                  for (int i = 0; i < N; ++i) {                          if (S.charAt(i) == ch[j]) {                                  ++count;                                  mp.add(S.charAt(i));                                  if (mp.size() == 5) {                    res = Math.max(res, count);                }            }                                      else if (j + […]

#include using namespace std;  bool ischar(char x){    if ((x >= ‘A’ && x = ‘a’ && x = ‘0’ && x