Largest word in dictionary by removing a few characters from the given string

Given a dictionary (list of words) and an input string K. Find the largest word in the dictionary which can be obtained by deleting a few characters in the input string K. Example: Dictionary: [tutorial, horizon, trial, zon] Input: taucdtorgibalbhsoariazaonzaqn Output: tutorial Dictionary: [tutorial, horizon, trial, zon], Input: attroialled Output: trial Approach: Please read – … Read more Largest word in dictionary by removing a few characters from the given string

Check if one string is a subsequence of another string.

Objective: Given two strings, write a program to check if any of the given string is a subsequence of another string. Example if given strings are A and B then EITHER A is subsequence of B OR B is subsequence of A, program should return true. Example: A = “horizon” B = “abhordizaonr” Output: true … Read more Check if one string is a subsequence of another string.

Most frequent word

Objective: Given an array of string, write a program to find the word in the array which appears the maximum number of times. Example: Input: [Algorithms, String, Integer, Integer, Algorithms, String, Integer, Algorithms, String, Algorithms] Most frequent word: Algorithms Naive Approach: Use the nested loops, compare the word from the outer loop with all the … Read more Most frequent word

Given an array, count the number of pairs with a given sum.

Objective: Given an array of integers, write a program to count all the pairs with the given sum. Example: Given array: [1, 5, 7, 1, -1], Sum= 6 Total Pairs: 3 Note: pairs are (1, 5), (5, 1) and (7, -1) Given array: [4, 5, 1, 2, 9, -2, -4], Sum= 5 Total Pairs: 2 … Read more Given an array, count the number of pairs with a given sum.

Two Sum Problem

Objective: Given an array of integers, and k. Write a program to find indexes of two elements in an array which sum is equal to K. Example: Given array: [5, 4, 7, 3, 9, 2], Sum = 13 Output: Found indexes are: 4 and 1 Given array: [1, 2, 3, 4, 5], Sum = 9 … Read more Two Sum Problem

Sum of distinct elements among two given sets

Objective: Given two sets of elements, find the sum of all distinct elements from the set. In other words, find the sum of all elements which are present in either of the given set.  This problem is also asked as – Find sum of non-overlapping numbers in two given sets. Example: Set 1 : [3, … Read more Sum of distinct elements among two given sets

Insert a node in the given sorted linked list.

Objective: Given a linked list in which nodes are sorted in ascending order. Write an algorithm to insert a given node into the linked list so that all the nodes in the list will maintain the sorted order. Example:  Given Linked List:  -> 2, Insert node: 6 New List:  -> 2 -> 6 Given Linked … Read more Insert a node in the given sorted linked list.

Number of Intervals in which given value lies

Objective: Given a list of intervals with start and end for each interval. You have given a value V, write an algorithm to find the number of intervals in which the value V lies.  Example: Given Interval: [[1,7], [3,10], [12,15]] Value : 6 lies in Intervals: 2 Given Interval: [[1,7], [3,10], [12,15]] Value : 11 … Read more Number of Intervals in which given value lies

Find the sum of overlapping elements in two sets

Objective: Given two sets of integers, write an algorithm to print the sum of overlapping elements in two sets. Note: Elements in each set are unique or there are no duplicates within a set. Example: Set 1: [2, 3, 1, 6] Set 2: [4, 0, 6, 7] Sum of overlapping elements: 12 Explanation: Common element … Read more Find the sum of overlapping elements in two sets

Given two coordinates, Print the line equation

Objective: Given two coordinates (x1, y1) and (x2, y2). Write a java program to construct a line equation that passes through the given coordinates. Line Equation: Given two coordinates (x1, y1) and (x2, y2) the line equation is  y – y1 = m(x – x1) where m is the slope of the line, where slope … Read more Given two coordinates, Print the line equation

Print all middle elements of the given matrix/2D array.

Objective: Given a two-dimensional array, write a program to print all middle elements of the array. Or In other words, print all the elements from the array except boundary elements. Example: int [][] grid = new int[][] { {1, 2, 3, 4, 8}, {5, 6, 7, 8, 9}, {9, 8, 7, 6, 7}, {5, 4, … Read more Print all middle elements of the given matrix/2D array.

Depth-First Search (DFS) in 2D Matrix/2D-Array – Recursive Solution

Objective: Given a two-dimensional array or matrix, Do the depth-First Search (DFS) to print the elements of the given matrix. Implement the Depth-first traversal in a recursive manner. Example: int [][] grid = new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output: Depth-First Traversal: … Read more Depth-First Search (DFS) in 2D Matrix/2D-Array – Recursive Solution

Hamming Distance between two given integers

Objective: Given two integers, write an algorithm to calculate the hamming distance between the integers.  Hamming Distance: Hamming distance between two integers is the number of positions at which the bits are different. Example: X = 2, Y = 4 Hamming distance: 2 2 = 0 1 0 4 = 1 0 0 There are … Read more Hamming Distance between two given integers

Breadth-First Search (BFS) in 2D Matrix/2D-Array

Objective: Given a two-dimensional array or matrix, Do the breadth-First Search (BFS) to print the elements of the given matrix. Implement a Breadth-first traversal in an iterative manner. Example: int [][] grid = new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output: Breadth-First Traversal: … Read more Breadth-First Search (BFS) in 2D Matrix/2D-Array