Category Archives: Hash

Find missing elements of a range

Given an array arr[0..n-1] of distinct elements and a range [low, high], find all numbers that are in range, but not in array. The missing elements should be printed in sorted order. Examples: Input: arr[] = {10, 12, 11, 15}, low = 10, hight = 15 Output: 13, 14 Input: arr[] = {1, 14, 11,… Read More »

Cuckoo Hashing – Worst case O(1) Lookup!

Background : There are three basic operations that must be supported by a hash table (or a dictionary): Lookup(key): return true if key is there in the table, else false Insert(key): adds the item ‘key’ to the table if not already present Delete(key): removes ‘key’ from the table Collisions are very likely even if we… Read More »

Count pairs with given sum

Given an array of integers, and a number ‘sum’, find the number of pairs of integers in the array whose sum is equal to ‘sum’. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1,… Read More »

Convert an array to reduced form

Given an array with n distinct elements, convert the given array to a form where all elements are in range from 0 to n-1. The order of elements is same, i.e., 0 is placed in place of smallest element, 1 is placed for second smallest element, … n-1 is placed for largest element. Input: arr[]… Read More »