Category Archives: Hash

Count Substrings with equal number of 0s, 1s and 2s

Given a string which consists of only 0s, 1s or 2s, count the number of substrings that have equal number of 0s, 1s and 2s. Examples: Input : str = “0102010” Output : 2 Explanation : Substring str[2, 4] = “102” and substring str[4, 6] = “201” has equal number of 0, 1 and 2… Read More »

Count maximum points on same line

Given N point on a 2D plane as pair of (x, y) co-ordinates, we need to find maximum number of point which lie on the same line. Examples: Input : points[] = {-1, 1}, {0, 0}, {1, 1}, {2, 2}, {3, 3}, {3, 4} Output : 4 Then maximum number of point which lie on… Read More »

Group Shifted String

Given an array of strings (all lowercase letters), the task is to group them in such a way that all strings in a group are shifted versions of each other. Two string S and T are called shifted if, S.length = T.length and S[i] = T[i] + K for 1 <= i <= S.length for… Read More »

Palindrome Substring Queries

Given a string and several queries on the substrings of the given input string to check whether the substring is a palindrome or not. Examples : Suppose our input string is “abaaabaaaba” and the queries- [0, 10], [5, 8], [2, 5], [5, 9] We have to tell that the substring having the starting and ending… Read More »

Print all subarrays with 0 sum

Given an array, print all subarrays in the array which has sum 0. Examples: Input: arr = [6, 3, -1, -3, 4, -2, 2, 4, 6, -12, -7] Output: Subarray found from Index 2 to 4 Subarray found from Index 2 to 6 Subarray found from Index 5 to 6 Subarray found from Index 6… Read More »

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 »