A binary tree is a tree where each node has a maximum of two children. We are given a binary tree that consists of only 0 and 1 as the node values. We have to find the number of levels of the binary tree that consists of at least one 1 and all the ones of that level must be present consecutively. Example Let's understand with the help of an example − Input 0 / \ 1 0 / ... Read More
A binary string is a string that only contains the binary numbers in it. In this problem, we are given a binary string, an array that represents the last index up to which we can flip the ones after starting from the ith index which will cost and cost for each index is given in another cost array. We have to perform some number of operations on the string to make the string completely zero. Example Let's understand the problem with the help of an example − Input string str = "101011" int arr[] = {1, 2, 2, 4, 5, ... Read More
We are given two arrays of the same length and we have to apply some operations to get the product of the first array with all elements maximum. The operations are to multiply or add any element of the second array only once to any element of the first array only once. We can add or multiply two different elements from the second array element to a single first array element. After all operations, we have to take the product of all the elements of the first array and return that. Example Let's understand the problem with the help of ... Read More
To rename files in Python, use the rename() method of the os module. The parameters of the rename() method are the source_address (old name) and the destination_address (new name). Install and Import the OS module To install the OS module − pip install os To import − import os Rename multiple files using rename() method The rename() method can be easily used to rename multiple files − Example import os # Function to rename multiple files def main(): i = 0 path="E:/amit/" for filename in os.listdir(path): my_dest ="new" + str(i) + ".jpg" my_source =path + filename my_dest =path ... Read More
Queue and Deque both are linear data structures that are defined in STL of C++ programming language. Queue works on the principle of the first in first out, the element added to the queue first will be removed first, on the other hand, deque has properties to add an element either at the first index or last index, and similarly, any one of them can be removed. We will see the code of both data structures to get the exact differences. Basics of Queue As we have seen above, the queue is based on the concept of the first in ... Read More
Binary Tree is a non-linear data structure. It has a maximum of two children and each node contain three things that are data value, left and right pointer. The top node is called the root node and the last node which does contain any children is called the leave node. In this problem, we have given a binary tree. It has N nodes in the range from 1 to N (both inclusive) and our task is to find the distance of each node of a binary tree from the root node using BFS. Examples Let's see examples with explanations below ... Read More
We are given an array of strings and each string represents a number that could be more than the range of the maximum integer limit. We have to find the largest and the smallest element from the given array. We cannot use the simple less than or greater than operators to check which string is greater as it won't works find with the string so we will make our own comparison function. Example Let's understand the problem with the help of an example − Input string arr[] = {"2", "3", "12", "23", "22", "0", "7"} Output The smallest element ... Read More
A substring is a string that can be achieved from the given string by removing some characters from the beginning and end of the string (possibly none or all). We are given a string and three characters and have to find the longest substring that contains all the three given characters in the sequence of c1, c2, and c3 starting with c1 and ending with c3. Also, the given characters may be the same but we string must contain different characters for each of them. Input string str = "abacdeab" char c1 = a char c2 = b char ... Read More
We are given a string of length S and given an another number n which represents the length of the strings which might contains S as the subsequence. We have to find the number of unique strings of length N which contains S as the subsequence, where a subsequence is the set of characters from the given string which may be all the characters or not and they not need to be continuous. Sample Examples Input string str = "xyz" int n = 3 Output 1 Explanation There is only one string of length 3 that contains the ... Read More
A subsequence is a string that can be achieved from another string by removing some (possibly none or all) from a given string that may not be continuous. We are given a string and have to find the number of subsequences that are greater than equal to the given string Y and less than equal to another given string Z. We will use dynamic programming to solve the problem as brute force will take exponential time. Brute Force Approach The brute forces approach is to find all the subsequences of the given string X and then check if they are ... Read More