Category: Searching

Find the missing value from Array B formed by adding some value X to Array A

def findHiddenMissing(arr1, arr2, N):            arr1.sort()    arr2.sort()        mp = {}            for i in range(0, N – 1):                                                               a = arr2[i] – arr1[i]                                b = arr2[i] – arr1[i + 1]                if (a != b):                                    if (a > 0):                if not a in mp:                    mp[a] = 1                else:                    mp[a] += 1                                if (b > 0):                if not b in mp:                    mp[b] = 1                else:                    mp[b] += 1                        else:                                    if (a > 0):                if not a in mp:                    mp[a] = […]

Continue Reading

Rabbit House | Google Kickstart 2021 Round A

#include using namespace std;  void solve(long long int r, long long int c,           vector& grid){    priority_queue        pq;      for (long long int i = 0; i < r; i++) {        for (long long int j = 0; j < c; j++) {            pq.push(make_pair(grid[i][j],                              make_pair(i, j)));        }    }      long long int res = 0;      while (!pq.empty()) {        long long int height = pq.top().first,                      i = pq.top().second.first,                      j = pq.top().second.second;        pq.pop();        if (height != grid[i][j])            continue;        if […]

Continue Reading

Lexicographically smallest string with given string as prefix

Given an array arr[] consisting of N strings and a string S if size M, the task is to find the lexicographically smallest string consisting of the string S as the prefix. If there doesn’t exist any string starting with prefix S then print “-1”.Examples:Input: arr[] = {“apple”, “appe”, “apl”, “aapl”, “appax”}, S = “app”Output: […]

Continue Reading

Rearrange Array to find K using Binary Search algorithm without sorting

#include using namespace std;  void Rearrange(int arr[], int K, int N){        int ans[N + 1];              int f = -1;      for (int i = 0; i < N; i++) {        ans[i] = -1;    }          K = find(arr, arr + N, K) – arr;                  vector smaller, greater;          for (int i = 0; i < N; i++) {                  if (arr[i] < arr[K])            smaller.push_back(arr[i]);                  else if (arr[i] > arr[K])            greater.push_back(arr[i]);    }      int low = […]

Continue Reading