A numeric string, str, is called a beautiful string if it can be split into a sequence arr of two or more positive integers, satisfying the following conditions −arr[i] - arr[i - 1] = 1, for any i in the index of sequence, i.e., each element in the sequence is more than the previous element.No element of the sequence should contain a leading zero. For example, we can split '50607' into the sequence [5, 06, 07], but it is not beautiful because 06 and 07 have leading zeros.The contents of the sequence cannot be rearranged.For example −If the input string ... Read More
Let us suppose you have given a dataset with various variables and data points thus in order to plot the cluster map for the given data points we can use Clustermaps class.In this example, we will import the wine quality dataset from the https://archive.ics.uci.edu/ml/datasets/wine+quality.import matplotlib.pyplot as plt import numpy as np import seaborn as sns sns.set(style='white') #Import the dataset wine_quality = pd.read_csv(‘winequality-red.csv’ delimeter=‘;’)Let us suppose we have raw data of wine Quality datasets and associated correlation matrix data.Now let us plot the clustermap of the data, row_colors = wine_quality["quality"].map(dict(zip(wine_quality["quality"].unique(), "rbg"))) g = sns.clustermap(wine_quality.drop('quality', axis=1), standard_scale=1, robust=True, row_colors=row_colors, cmap='viridis')Plot the ... Read More
Given a Binary Tree, the task is to check whether it is a Full Binary Tree or not. A Binary Tree is said to be a Full Binary Tree if every node has zero or two children.For ExampleInput-1Output:1Explanation: Every node except the leaf node has two children, so it is a full binary tree.Input-2: Output:0Explanation: Node 2 has only one child, so it is not a full binary tree.Approach to Solve this ProblemTo check whether a given binary tree is full or not, we can check recursively for the left subtree and right subtree.Input a given Binary Tree having nodes and its children.A ... Read More
Given a non-negative number, the task is to find its smallest digit.For exampleInput:N = 154870Output:0Explanation: In the given number '154870', the smallest digit is '0'.Approach to Solve this ProblemThe simplest approach to solve this problem is to extract the last digit in the given number using the remainder theorem. While traversing the number, we will check if the extracted digit is less than the last digit, then return the output.Take a number n as the input.An integer function smallest_digit(int n) takes 'n' as the input and returns the smallest digit in the given number.Now initialize min as the last digit of the ... Read More
Circle Sort is an interesting sorting algorithm to sort a given array of elements. The algorithm compares the elements of the array diametrically and once the elements in one part is sorted, then continuously sort the other end of the array diametrically.ExampleLet us visualize the circle sort for an array. Let us suppose we have an array with 6 elements.Input:N = 6arr [ ] = { 2, 1, 5, 8, 7, 9 }When we draw concentric circles for each array element, then it will appear as followsOutput:1 2 5 7 8 9Explanation: After sorting the elements in the array using Circle ... Read More
A Linked List is a linear data structure in which each node is having two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.Let us assume that we have a linked list such that each node contains a random pointer which is pointing to other nodes in the list. The task is to construct the list with the same as the original list. Copying the list from the original list which is having some random pointer is called a 'Deep Copy' of the linked list.For ... Read More
Given a 32-bit Unsigned Binary Number, the task is to count the set bits, i.e., '1's present in it.For ExampleInput:N = 00000000000000100111Output:4Explanation: Total set bits present in the given unsigned number is 4, thus we will return the output as '4'.Approach to Solve this ProblemWe have given an unsigned 32-bit binary number. The task is to count how many '1's present in it.To count the number of '1's present in the given binary number, we can use the inbuilt STL function '__builin_popcount(n)' which takes a binary number as the input parameter.Take a binary number N as the input.A function count1Bit(uint32_t n) ... Read More
Given a string 'a' and a character 'char', the task is to print the distance of 'char' from each character of the given string. The size of the distance array is same as the size of the string, since we have to find the distance of the character from each character of the given string.For ExampleInput-1:a = “tutorialspoint”char = “o”Output: [ 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3]Explanation: In the given string, the distance of the character from each character of the given string is [3, 2, 1, 0, 1, 2, 3, 4, 5, 6, ... Read More
Let us suppose we have a Binary Tree having a root node and its left child and right child. The task is to find the total sum of leaf nodes of the tree which are left to its parent node.For ExampleInput-1: Output:15Explanation: In the given input Binary Tree, the sum of all the left leaf nodes is 9+4+2 = 15. So, the output is 15.Approach to Solve this ProblemWe have a Binary Tree and the task is to find the sum of all the leaf nodes which are left to its parent.The recursive approach to solve this problem is to ... Read More
A Linked List is a linear data structure in which each node has two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.Let us assume that we have a linked list such that each node contains a random pointer which is pointing to the other nodes in the list. The task is to find the node at which two linked lists intersect each other. If they don't intersect, then return NULL or empty as output.For ExampleInput-1: Output:2Explanation: Since the given linked ... Read More