In this article, we will explain the concept of rolling hash and find the longest common substring using binary search and rolling hash. We will also provide a C++ code implementation for the same. Rolling Hash of a String Problem Statement Algorithm to Find the Longest Common Substring C++ Code Implementation Time and Space Complexity Rolling Hash of a String Rolling hash is a cryptographic technique used to calculate the hash value of a string. In this, we ... Read More
In this article, we need to identify the three minimum elements in an array: minimum, second minimum, and third minimum. We will look at two different approaches to solve this problem and compare their time complexity. Problem Statement Given an array of n elements, we need to find the first, second, and third minimum elements in the array. The first minimum is the minimum of the array's elements, the second is a minimum but larger than the first, and the third is a minimum but larger than the second. Let us see the following example scenario to understand the problem ... Read More
We are given a string and need to move all characters at even positions to the end of the string while maintaining the original order of both even-positioned and odd-positioned characters. The transformation should be done in-place (without using extra space) and in O(n) time. Let's look at a few example scenarios to understand the problem clearly. Scenario 1 Input: T1u2t0o6r2i0a0l4s Output: Tutorials12062014 Explanation: Odd-positioned characters are T u t o r i a l s. Even-positioned characters are 1 2 0 6 2 0 1 4. We place the even-positioned characters at the end while keeping their ... Read More
A singly linked list is a fundamental data structure that consists of nodes, where each node contains a data field and a reference to the next node in the linked list. The next of the last node is null, indicating the end of the list. The given task is to rearrange the linked list in such a way that all the vowel nodes precede the consonants while maintaining the order of their arrival. Scenario Input: a -> b-> c -> d-> e-> f-> g-> h-> i Output: a-> e-> i-> b-> c-> d-> f-> g-> h To solve this problem, we ... Read More
In this article, we will learn to solve a popular problem that involves finding the closest element in a binary search tree (BST) to a given target value. There are two methods to solve this problem: Brute Force Method (Inorder Traversal) Optimized Method (DFS Traversal) Before moving to the solution, let's understand the problem statement in detail. Find the Closest Element in Binary Search Tree Given a binary search tree (BST) and a target value K, the task is to find a node in BST, whose value is ... Read More
A prime number is a number greater than 1 that is divisible only by 1 and itself. Given a number N, our task is to print all alternate prime numbers up to N. This means we skip every second prime number and only print the 1st position, 3rd position, 5th position, and so on. Let's look at the example scenarios to understand the problem clearly: Scenario 1 Input: N = 15 Output: 2 5 11 Explanation: Prime numbers up to 15 are: 2, 3, 5, 7, 11, 13 Taking alternate primes (1st, 3rd, 5th): 2, 5, 11 Scenario 2 ... Read More
A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In, First Out (FIFO). In a queue, the element added first will be removed first. The end where elements are added is called the rear, and the end where elements are removed is called the front. Scenario 1 Let us see an example, scenario: Input: [150, 300, 450, 600] After removing an element from the queue: Output: [300, 450, 600] The following are the ways to implement a queue in Java: Using Array ... Read More
In this article, we have a line and its coordinates are given. Our task is to check if this line passes through the origin or not. The basic two-point form of a straight line is given by: $$ \frac{y - y_1}{y_2 - y_1} = \frac{x - x_1}{x_2 - x_1} + c $$ For the above line to pass through the origin, put x = 0, y = 0, and c = 0. The formula for a straight line to pass through the origin can be given by: $$ y_1(x_2 - x_1) = x_1(y_2 - y_1) $$ Here are some ... Read More
In this article, we will learn how to find the square root of a number up to a given precision by using binary search algorithm and implement it in C++. Before dive into the concept, make sure that you have a basic understanding of binary search algorithm. Square Root of a Number Upto a Given Precision In this problem, you are given a positive floating point number N and a positive integer P. The task is to find the square root of N up to P decimal places using binary search. Scenario 1 Input: N = ... Read More
In this article, we will explain a beginner level problem that involves finding answers for questions by checking for vowels in the question text. We will implement a C++ program that solves this problem. Let's break down the problem statement below. Find Answers by Vowel Checking Amal and Bimal are playing a game. Amal will ask any questions whose answers will be either "Yes" or "No". If the question’s last letter is a vowel, then Bimal will answer "Yes" otherwise "No". You are given a string containing the question that Amal asks. Your task is to determine the answer ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP