Suppose we have a list of words representing an English Dictionary, we have to find the longest word in the given word list that can be built one character at a time by other words in words. If there is more than one possible answer, then return the longest word with the smallest lexicographical order. If there is no answer, then return the empty string.So, if the input is like ["h", "he", "hel", "hell", "hello"], then the output will be "hello"To solve this, we will follow these steps −trie := a new mapDefine a function insert(). This will take wordnow ... Read More
In Bitonic Sort, the comparision is in predefined sequence (Bitonic sequence), not dependent on the data to be sorted. Let us see an example for Bitonic Sort Java program −Example Live Demopublic class Demo{ void compare_swap(int my_arr[], int i, int j, int direction){ if ((my_arr[i] > my_arr[j] && direction == 1) || (my_arr[i] < my_arr[j] && direction == 0)){ int temp = my_arr[i]; my_arr[i] = my_arr[j]; my_arr[j] = temp; } } void merge_vals(int my_arr[], int low, int cnt, int direction){ ... Read More
Suppose we want to design a HashMap without using any built-in hash table libraries. There will be different functions as follows −put(key, value) − This will insert a value associated with key into the HashMap. If the value already exists in the HashMap, update the value.get(key) − This will return the value to which the specified key is mapped, otherwise -1 when this map contains no mapping for the key.remove(key) − This will Remove the mapping for the value key if this map contains the mapping for the key.So, if the input is like After initialization, call the put and ... Read More
Suppose we want to design a HashSet data structure without using any built-in hash table libraries. There will be different functions like −add(x) − Inserts a value x into the HashSet.contains(x) − Checks whether the value x is present in the HashSet or not.remove(x) − Removes x from the HashSet. In case the value does not exist in the HashSet, do nothing.So, to test it Initialize the hash set, then call add(1), add(3), contains(1), contains(2), add(2), contains(2), remove(2), contains(2)., then the output will be true (1 is present), false (2 is not present), true (2 is present), false (2 is ... Read More
Following is the program for Recursive Binary Search in Java −Example Live Demopublic class Demo{ int rec_bin_search(int my_arr[], int left, int right, int x){ if (right >= left){ int mid = left + (right - left) / 2; if (my_arr[mid] == x) return mid; if (my_arr[mid] > x) return rec_bin_search(my_arr, left, mid - 1, x); return rec_bin_search(my_arr, mid + 1, right, x); } return -1; } ... Read More
Suppose we want to design a class to find the kth largest element in a stream. It is the kth largest element in the sorted order, not the kth distinct element.The KthLargest class will have a constructor which accepts an integer k and an array nums, that will contain initial elements from the stream. For each call to the method KthLargest.add, will return the element representing the kth largest element in the stream.So, if the input is like k = 3, initial elements = [4, 5, 8, 2], then call add(3), add(5), add(10), add(9), add(4). , then the output will ... Read More
Following is an example for Anagram Substring Search in Java −Example Live Demopublic class Demo{ static final int max_val = 256; static boolean compare_vals(char my_arr_1[], char my_arr_2[]){ for (int i = 0; i < max_val; i++) if (my_arr_1[i] != my_arr_2[i]) return false; return true; } static void search_subs(String my_pattern, String my_text){ int pat_len = my_pattern.length(); int txt_len = my_text.length(); char[] count_pat = new char[max_val]; char[] count_txt = new char[max_val]; for ... Read More
Suppose we have an array of non-negative integers called nums, the degree of this array is actually the maximum frequency of any one of its elements. We have to find the smallest possible length of a contiguous subarray of nums, that has the same degree as nums.So, if the input is like [1, 2, 2, 3, 1], then the output will be 2, this is because the input array has a degree of 2 because both elements 1 and 2 appear twice. The subarrays that have the same degree − [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, ... Read More
Suppose we have a string s, we have to find the count of contiguous substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively. If substrings occur multiple times are counted the number of times they occur.So, if the input is like "11001100", then the output will be 6, as the substrings are "1100", "10", "0011", "01", "1100", "10".To solve this, we will follow these steps −Define an array cnt of size 2 and fill this with 0res := 0for initialize i := 0, when i ... Read More
The Comb Sort in Java eliminates the smaller values situated to the end of the list and the inversions are removed oby by one. Let us see an example −Example Live Demoimport java.util.Arrays; public class Demo{ void comb_sort(int nums[]){ int len_gap = nums.length; float shrink_val = 1.3f; boolean swap = false; while (len_gap > 1 || swap) { if (len_gap > 1) { len_gap = (int)(len_gap / shrink_val); } swap ... Read More