Saturday, 4 Dec 2021
Category: HashSet

Generate a permutation of first N natural numbers from an array of differences between adjacent elementsGiven an array arr[] consisting of (N – 1), the task is to construct a permutation array P[] consisting of the first N Natural Numbers such that arr[i] = (P[i +1] – P[i]). If there exists no such permutation, then […]

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 + […]

import java.io.*;import java.util.*;  class GFG {              static int getCountPairs(int arr[],                             int N, int S)    {        HashMap mp            = new HashMap();                          for (int i = 0; i < N; i++) {                                      if (!mp.containsKey(arr[i]))                mp.put(arr[i], 0);              mp.put(arr[i],                   mp.get(arr[i]) + 1);        }                          int twice_count = 0;                          for (int i = 0; i < N; i++) {                                      if (mp.get(S - arr[i])                != null) {                                  twice_count += mp.get(                    S - arr[i]);            }              if (S - arr[i] == arr[i])                twice_count--;        }                  return twice_count / […]

#include using namespace std;  void countStringPairs(string a[], int n){          int ans = 0;              for (int i = 0; i < n; i++) {        for (int j = i + 1; j < n; j++) {                                      string p = a[i], q = a[j];                          if (p[0] != q[0]) {                  swap(p[0], q[0]);                int flag1 = 0;                int flag2 = 0;                                                  for (int k = 0; k < n; […]