In programming, to search for anything from an array, linked List, or any other data structures we have a few search algorithms, one of which is binary search. In binary search, the prerequisite is that the data should be sorted. In binary search, we follow the divide and conquer approach in which we divide the data by applying some conditions and then perform the operation on that data only. In this way, we reduce the time complexity. For example, if we have an array of elements {20, 44, 45, 54, 67, 88, 91} and we want to find 44 then ... Read More
In Golang, like other programming languages, we can find the union of two arrays. The union of two arrays is a list that consists of elements that lie in array A and the elements that lie in array B and common elements that lie in both arrays. For example, we have two arrays listed below A = {2, 9, 5, 7, 3} B = {5, 8, 7 2, 1} The union of the above arrays will be Union = {1, 2, 3, 5, 7, 8, 9} Method 1 In this method, we are going to find the union of two ... Read More
In programming languages, we can create 2−Dimensional Matrices and store elements in them. The 2−Dimensional metric is a data structure that has rows and columns. In this article, we are going to see the two different logic to find the maximum no. of ones in a sorted row of the matrix. For example, we have below the matrix {0, 1, 1, 1}, no. of 1s = 3 {0, 0, 1, 1}, no. of 1s = 2 {1, 1, 1, 1}, no. of 1s = 4 {0, 0, 0, 0}, no. of 1s = 0 In the ... Read More
In programming, there is a problem statement asked in interviews to find the duplicate number in an array of size N + 1. Also, there will be only one duplicate element in the array. The elements will be between 1 to N. For example, Array = {1, 3, 2, 1, 4} 1 is the duplicate element in the above array. Algorithm Step 1: Import the required packages at the top using the import keyword. Step 2: Then the main function will get run first. First, we are declaring and initialize the array. Now, we are calling the function to ... Read More
In programming, there are different data structures to store data. There are two different types of data structures linear and non −linear. Array, stack, queue, and linked list are linear data structures. Binary trees, trie, etc are non − linear data structures. In this article, we are going to explore level order traversal on one of the non − linear data structures i.e. binary tree. Level Order Traversal In level order traversal, of a binary tree we start from the root node and then traverse the children nodes and move to the children of children nodes. In this way, ... Read More
In programming, there is a coding problem of binary tree that gets asked very frequently in interviews and the problem statement is to find the left view of a binary tree. If we try to understand the problem statement more than what exactly the left view is then we can explain it in a way that all the nodes are visible when you see them by standing on the left side of the tree. Illustration Let us understand more with the help of an example. Suppose we have a below tree and if we stand on the left side ... Read More
Graph is a data structure that consists of edges or we can say nodes and vertices. Vertices are the lines between the nodes. To traverse all these nodes we have different traversal algorithms. In this article, we will discuss, Breadth − first search or we can say BFS. In Breadth − first search, we first start from one node and move to another till the dead end comes. Example If we start from node 1 then it will first visit node 2 and node 4 first. Then from node 2, we will visit node 3. In this way, the ... Read More
In programming, to make the code more modular, usable, and readable we break the code into different functions. For example, we have to swap two numbers at different places so instead of swapping in place we create a different function and call it everywhere. In some use cases, we need a function that returns multiple values at once. For this, we have a data structure called arrays in programming that is nothing but a collection of multiple values of the same type. In this tutorial, we are going to learn about how to return an array in function as an ... Read More
In programming, to make the code more modular, usable, and readable we break the code into different small blocks and these blocks are known as functions. A string is a data structure consisting of a set of alphabets. In solving any programming problem we want that the function should return some set of alphabets then we create a string in the function that the function returns in the end. This tutorial will explain this concept with the help of two examples. In the first example, we will define two strings and create a function that will return the string ... Read More
In this article, the given task is to find the number of unique words in a text file. In this Python article, using two different examples, the approaches to finding the unique words in a text file and their count are given. In the first example, given words from a text file are fetched, and then their unique set is made before counting these unique words. In example 2, first the list of words is created, then it is sorted. After this from this sorted list, the duplicates are removed and finally, the unique word left in the file ... Read More