Red Black Tree is a Self-Balanced Binary Search Tree in which each node of the tree is colored with either Red or Black. There are three types of operations we can perform on a Red Black Tree – Searching, Insertion and Deletion.Let us suppose we have to insert an element in the following Red Black Tree.To insert an element in a red-black tree the idea is very simple − we perform insertion just like we insert in a regular binary tree. We start off from the root node by checking the color of the node and insert it into a ... Read More
Pandigital Number − In Mathematics, a Pandigital number is an integer that in a given base has among its significant digits each digit used in the base at least once.Pandigital numbers are the integers in which each digit is used as the base at least one time.For example, 1245678 is a pandigital number.Approach to solve this problemTake Input a number and a base.Check the base if it is less than 2 and greater than 10 then return 1 otherwise check the number if it is pandigital or not.An Integer function is_pandigital(long long n, int base) takes a number and a ... Read More
Let’s Suppose we have given two strings ‘a’ and ‘b. We have to check that the given two strings are anagram of each other or not. Two Strings are said to be anagram of each other if one string contains the same character as another.For ExampleInput-1 −a= anagram b= gnaramaOutput −TrueExplanation − String ‘gnarama’ has the same character as String ‘anagram’ has. Hence we return True.Input-2 −a= programmer b= mprogretmrqpOutput −FalseExplanation − String ‘b’ has more characters than the string ‘a’ and thus we can say that the length of the string is different. Thus we return False.The approach used ... Read More
Given an array of sorted numbers containing only 0s and 1s, find the transition point. A transition point is the index of the first ‘1’ appearing in the array. For example, Input-1 −N = 6 arr[ ] = {0, 0, 0, 0, 1, 1}Output −4Explanation − Since in the given array containing 0’s and 1’s we can see that the element at the index ‘4’ is having the number ‘1’.Input-2 −N = 5 arr[ ] = {0, 0, 1, 1, 1}Output −2Explanation − In the given array containing 0’s and 1’s, we can see that the element at the index ... Read More
Floyd Cycle is one of the cycle detection algorithms to detect the cycle in a given singly linked list.In the Floyd Cycle algorithm, we have two pointers that initially point at the head. In Hare and Tortoise’s story, Hare moves twice as fast as Tortoise, and whenever the hare reaches the end of the path, the tortoise reaches the middle of the path.AlgorithmInitialize Hare and Tortoise at the head node of the List.Initially, the hare moves twice as fast as the tortoise.Move the hare and tortoise both and find if the hare reaches the end of the Linked List, return ... Read More
Suppose we have an array of integers of size N and a key K. Our task is to print the top K most frequent element of the array. For example, Input-1 −N = 6 K = 2 arr[ ] = {1 ,1, 1, 2, 2, 3}Output −1 2Explanation − In the given array of integers, the top K=2 elements whose frequency is most in the array are {1, 2}.Input-2 −N = 2 K = 1 arr[ ] = {1, 2}Output −1Explanation − In the given array of integers, the top K=1 elements whose frequency is most in the array are ... Read More
Let’s suppose we have an array of integers of size N. The task is to find the most frequent element present in the given array of integers. For example, Input-1 −N = 8 A[ ] = {1, 2, 4, 3, 3, 1, 1, 5}Output −1Explanation − In the given array of integers, the most appearing number is ‘1’. Thus the output is ‘1’.Input-2 −N = 6 A[ ] = {1, 4, 4, 4, 1, 1}Output-a −1Output-b −4Explanation: In the given array of integers, the most appearing number is ‘1’ and ‘4’. Thus we can return the output to any one ... Read More
Let’s suppose we have given an array of unsorted integers. The task is to find the positive missing number which is not present in the given array in the range [0 to n]. For example, Input-1 −N = 9 arr = [0, 2, 5, 9, 1, 7, 4, 3, 6]Output −8Explanation − In the given unsorted array, ‘8’ is the only positive integer that is missing, thus the output is ‘8’.Input-2 −N = 1 arr = [0]Output −1Explanation − In the given array, ‘1’ is the only one positive integer which is missing, thus the output is ‘1’.Approach to solve ... Read More
A linked list is a linear data structure that has multiple nodes that are connected with each other. Each node consists of two fields Data Field and address of the next node. Let us assume we have given a singly linked list the task is to find the kth node from the end in a given singly linked list. For example, Input −1→2→3→4→7→8→9 K= 4Output −Node from the 4th Position is − 4Explanation − Since in the given singly linked list ‘4th’ node from the end is ‘4’, we will return the output as ‘4’.Approach to solve this problemInitially, we ... Read More
Given a string ‘s’, the task is to find the first unique character which is not repeating in the given string of characters and return its index as output. If there are no such characters present in the given string, we will return ‘-1’ as output. For example, Input-1 −s = “tutorialspoint”Output −1Explanation − In the given string “tutorialspoint”, the first unique character which is not repeating is ‘u’ which is having the index ‘1’. Thus we will return ‘1’ as output.Input-2 −s = “aaasttarrs”Output −-1Explanation − In the given string “aaasttarrs’, there are no unique characters. So, we will ... Read More