Category: cpp-map

Max count of N using digits of M such that 2 and 5, and, 6 and 9 can be treated as same respectively

  import java.util.HashMap;import java.util.Map;  public class GFG {                  int solve(int n, String str)    {                          HashMap mymap            = new HashMap();                  int len = str.length();                  for (int i = 0; i < len; i++) {            char c = str.charAt(i);                          if (c == '5')                c = '2';                          else if (c == '9')                c = '6';                                                  int c_int = Integer.parseInt(                String.valueOf(c));                          if (mymap.containsKey(c_int))                mymap.put(                    c_int, mymap.get(c_int) + 1);            else                mymap.put(c_int, 1);        }                          HashMap rems            = new HashMap();                          while (n > 0) {                                      int rem […]

Continue Reading

Test Case Generation | Set 6 (Random Unweighted Binary Tree)

  #include using namespace std;    vector generateBinaryTree(int n){                        vector options = { 0, 1, 2 };                        map mp;                queue q;            q.push(1);            vector v;                int count = 1;                mp[1] = 1;            while (!q.empty() or count < n) {                                            int front;                  if(!q.empty()) {                                                    front = q.front();                                                                q.pop();        }                                                          int numberOfChilds          = options[rand() % (options.size())];                            if (count >= n)            continue;                                        while (numberOfChilds–) {                                                                int child = rand() % n + 1;                                        while (mp[child]) {                child++;                if (child > n) {                    child = 1;                }            }                                        count++;                            mp[child] […]

Continue Reading

Count pairs from an array having GCD equal to the minimum element in the pair

  #include using namespace std;  int CountPairs(int arr[], int N){        int res = 0;              map mp;          for (int i = 0; i < N; i++) {        mp[arr[i]]++;    }        for (auto p : mp) {                  int x = p.first;                          int y = p.second;                  if (x == 1) {                          res += N - 1;            continue;        }                  res += (y * (y - 1)) / 2;                          for (int j = 2;             j

Continue Reading

Sum of absolute differences of indices of occurrences of each array element | Set 2

  import java.util.*;  class GFG {              static class pair {        int count, prevIndex;                  pair(int count, int prevIndex)        {            this.count = count;            this.prevIndex = prevIndex;        }    }                  static void findSum(int[] arr, int n)    {                        Map map = new HashMap();                          int[] left = new int[n];        int[] right = new int[n];                  for (int i = 0; i < n; i++) {                          if (!map.containsKey(arr[i])) {                                                                  left[i] = 0;                map.put(arr[i], new pair(1, i));            }                                      else {                pair tmp = map.get(arr[i]);                left[i] = (tmp.count)                              * […]

Continue Reading

Sum of all vertical levels of a Binary Tree

   class TreeNode:     def __init__(self, val =”,                  left = None, right = None):         self.val = val         self.left = left         self.right = right    def getDecimal(arr):                       arr.sort()                       ans = 0             for i in range(len(arr)):         ans

Continue Reading