Found 1862 Articles for Data Structure

Golomb sequence

Divya Sahni
Updated on 25-Jul-2023 12:38:15

448 Views

Golomb Sequence − The Golomb Sequence is a non-decreasing sequence of integers where the value of the nth term is the number of times the integer n appeared in the sequence. Some terms of Golomb sequence are, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, … Here as we can see, the 5th term is 3 and 5 also appears 3 times in the sequence. The 6th term is 4 and 6 also appears 4 times in ... Read More

Print all Palindromic Levels of a Binary Tree

Shubham Vora
Updated on 22-Jul-2023 12:17:34

98 Views

In this problem, we will print each palindromic level of the given binary tree. We can use the breadth−first search algorithm to traverse each level of the given binary tree and check whether a particular level of the binary tree is palindromic. If yes, we print the level in the output. Problem statement − We have given a binary tree, and we need to print all palindromic levels of the binary tree. If any particular level of the binary tree contains the same numbers from left to right and right to left, we can say that the current level is ... Read More

Front and Back Search in unsorted array

Divya Sahni
Updated on 25-Jul-2023 12:35:32

184 Views

Unsorted Array − An array is a data structure consisting of a collection of elements of the same type. An unsorted array is such a structure where the order of elements is random, i.e. on insertion, the element is added to the last irrespective of the order of previous elements and searching in such an array is not helped by any search algorithm because of lack of a pattern of the positioning of elements. Searching − Searching in an array means finding a particular element in the array which can be either returning the position of a desired element or ... Read More

Print all Nodes Except Rightmost Node of Every Level of the Binary Tree

Shubham Vora
Updated on 22-Jul-2023 12:12:36

55 Views

In this problem, we will print all the nodes of the binary tree except the rightmost node of each level. We will use the level order traversal to traverse the binary tree, and we won’t print the last node of each level, which is the rightmost node. Problem statement − We have given a binary tree containing different nodes. We need to print all nodes of the binary tree except right most node. Sample examples Input  7 / \ ... Read More

Minimize the Maximum difference between Adjacent Elements in an Array

Shubham Vora
Updated on 22-Jul-2023 12:06:48

389 Views

In this problem, we will minimize the maximum difference between adjacent elements by removing any M elements from the array. The naïve approach to solving the problem is to pick total N − M array elements and check which set contains the minimum or maximum adjacent difference. The optimized approach uses the queue data structure to solve the problem. Problem statement : We have given an sorted array of numbers in sorted order. We have also given M. We need to remove M elements from the array such that we can minimize the maximum difference between the adjacent array ... Read More

Find n-th Fortunate Number

Divya Sahni
Updated on 25-Jul-2023 12:32:59

109 Views

Fortunate Numbers − It is the smallest integer m > 1 such that, for a given positive integer n, pn# + m is a prime number, where pn# is the product of the first n prime numbers. For example, for calculating the third fortunate number, first calculate the product of the first 3 prime numbers (2, 3, 5) i.e. 30. Upon adding 2 we get 32 which is an even number, adding 3 gives 33 which is a multiple of 3. One would similarly rule out integers up to 6. Adding 7 gives, 37 which is a prime number. Thus, ... Read More

Iterative Method to Find Height of Binary Tree

Shubham Vora
Updated on 21-Jul-2023 21:51:02

298 Views

The binary tree is a data structure. Each node of the binary tree contains either 0, 1, or 2 nodes. So, the binary tree can contain multiple levels. Here, we need to write the iterative code using the loops to find the height of the binary tree. The total number of levels in the binary tree represents the height of the binary tree. Alternatively, we can say that the maximum depth of the binary tree from the root node is the height of the binary tree. Problem statement − We have given a binary tree. We need to ... Read More

Check if the given two numbers are friendly pairs or not

Divya Sahni
Updated on 25-Jul-2023 12:25:24

483 Views

Friendly Numbers − According to number theory, friendly numbers are two or more numbers having the same abundancy index. Abundancy Index − Abundancy index of a natural number can be defined as the ratio between the sum of all the divisors of the natural number and the natural number itself. The abundancy of a number n can be expressed as $\mathrm{\frac{\sigma(n)}{n}}$ where $\mathrm{\sigma(n)}$ denotes the divisor function equal to all the divisors of n. For example, the abundancy index of the natural number 30 is, $$\mathrm{\frac{\sigma(30)}{30}=\frac{1+2+3+5+6+10+15+30}{30}=\frac{72}{30}=\frac{12}{5}}$$ A number n is said to be a ‘friendly number’ if there exists a ... Read More

How to Efficiently Implement k Queues in a Single Array?

Shubham Vora
Updated on 21-Jul-2023 21:47:38

193 Views

In some cases, we need to implement our own data structure for better usability and customization. Here, we need to implement the K Queues using the single array. The first solution that comes to mind is dividing the array into N/K parts and using each part of the array as a queue. Here, N is the array length. The problem with this solution is that we can’t utilize the array's space properly. If the array is not full, but any Mth queue indexes are full, we can’t insert an element to the Mth queue. So, we need an optimized approach. ... Read More

Find the Number of 'X' Total Shapes

Shubham Vora
Updated on 22-Jul-2023 14:50:19

99 Views

In this problem, we need to find the total number of ‘X’ shapes in the given matrix. We can construct the single ‘X’ shape using 1 or more adjacent ‘X’ elements. We can use the DFS (depth−first search) technique to solve the problem. For each ‘X’ element, we can find all adjacent elements using DFS and count it as a single ‘X’ shape. If we find a new ‘X’, we find its adjacent again. Here, we will use the iterative and recursive DFS to find the total number of ‘X” shapes. Problem statement − We have given a matrix[] of ... Read More

Advertisements