Found 1862 Articles for Data Structure

Implementation of a Hypergraph

Shubham Vora
Updated on 02-Aug-2023 13:38:36

148 Views

In this tutorial, we will learn to implement the hypergraph in C++. Definition − The hypergraph is a special version of the graph. In which the single can connect 2 or more vertices. In a normal graph, the single edge can connect only 2 vertices, but a hypergraph is a generalization of the graph and can be used to connect more than 2 vertices with the single edge. In the hypergraph, the edge is called the hyperedge. We can represent the hypergraph with H(E, V), where E is a hyperedge and v is the set of vertices connected by the ... Read More

Find the Largest Multiple of 3 (Using Queue)

Shubham Vora
Updated on 02-Aug-2023 13:31:59

89 Views

In this problem, we will find the largest multiple of 3 using the array elements. The naïve approach is that generate all possible combinations of array elements and check whether it is divisible by 3. In this way, keep track of the maximum possible multiple of 3. The efficient approach is using three queues. We can use queues to store elements based on the remainder after dividing it into 3. After that, we can remove some elements from the queue to make a multiple of 3 from the remaining array elements. Problem statement − We have given an array ... Read More

Find the K Closest Points to Origin using Priority Queue

Shubham Vora
Updated on 02-Aug-2023 13:29:05

143 Views

In this problem, we will find the K nearest point from the origin in the 2D plane from the given N points. We can use the standard Euclidean distance formula to calculate the distance between the origin and each given point. After that, we can store the points with distance in the array, sort the array according to the distance, and take the first K points. However, we can also use the priority queue to store the 2D points according to their distance from the origin. After that, we can deque the queue for K times. Problem statement − ... Read More

Count of Prime Factors of N to be Added at each Step to Convert N to M

Shubham Vora
Updated on 02-Aug-2023 13:22:47

43 Views

In this problem, we will convert the number N to M by adding the one prime factor of N to itself and updating it in each operation. We will use the breadth−first search algorithm to solve the problem. We will find the prime factor of each updated N and insert it into the queue after adding it to the prime factor of N. Also, we will define functions to find the smallest prime factor of a particular number. Problem statement − We have given N and M integer values. We need to count the minimum number of operations required to ... Read More

Count of Array Elements whose Order of Deletion Precedes Order of Insertion

Shubham Vora
Updated on 02-Aug-2023 13:36:59

54 Views

In this problem, we will count the number of elements removed from the array before they are inserted in the array. The logical part for solving the problem is that check for all numbers that its position in the remove[] array before its position in the insert[] array. If yes, we can count that particular element of the remove[] array in the answer. However, we will use the map data structure to improve the performance of the code. Problem statement − We have given an insert[] and remove[] arrays containing first N integers. The insert[] array represents the order of ... Read More

Construct Complete Binary Tree from its Linked List Representation

Shubham Vora
Updated on 02-Aug-2023 13:11:14

413 Views

In this problem, we will convert the linked list to the complete binary tree. We can assume the linked list as an array. The pth element of the linked list is the parent node of the binary tree's 2*p + 1 and 2*p + 2 elements. So, we can traverse through each element of the linked list and construct the binary tree. Problem statement − We have given a linked list containing N nodes. We need to construct the complete binary tree from the given linked list. Also, print the in−order traversal of the binary tree. Note − In the ... Read More

Check if the given Permutation is a Valid BFS of a Given Tree

Shubham Vora
Updated on 02-Aug-2023 13:05:45

145 Views

In this problem, we will check whether we can get the permutation of 1 to N elements given array using the BFS (Breadth−first search) traversal with the given binary tree. Here, we will traverse the tree and find all possible permutations using the BFS traversal. After that, we can check whether any BFS traversal result matches the array permutation. Problem statement − We have given an array of size containing the first N positive integers in random order. Also, we have given a tree containing the first N numbers as a tree node. We need to check whether we ... Read More

Check if the End of the Array can be Reached from a given Position

Shubham Vora
Updated on 02-Aug-2023 12:58:23

58 Views

In this problem, we will check whether we can reach the array’s end by moving back or ahead by nums[p] moves from the current position. The naïve approach to solve the problem is to check all possibilities to move into the array and see if we can reach the array’s end. Another approach is using the queue and BFS traversal in the array. In this tutorial, we will learn both approaches to check whether we can reach to the end of the array. Problem statement − We have given a nums[] array containing the positive integers. Also, we have given ... Read More

Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged

Sonal Meenu Singh
Updated on 01-Aug-2023 09:43:27

690 Views

Introduction In this tutorial, we introduce an approach to sorting an alphanumeric string such that the position of alphabets and numbers remains unchanged. In this approach, we use C++ functions and take an input string containing characters and numbers. Generate the string without changing the alphabet and number positions. The newly sorted string contains the shuffled characters in alphabetical order, and arranging the numbers will keep their positions the same. Example 1 String = “”tutorials43points” Output = aiilnoopr34sstttu In the above example, the input string is rearranged without changing the position of the number 32. The characters are ... Read More

Repeated Character Whose First Appearance is Leftmost

Sonal Meenu Singh
Updated on 01-Aug-2023 09:41:45

115 Views

Introduction In this tutorial, we will develop an approach to finding repeated characters in a string whose first appearance is leftmost. This means the character first appeared at the beginning of the string. To find out whether the first character repeats or not, we traverse the whole string and match each character to the first character of the string. To resolve the task we use the find(), length(), and end() functions of the C++ programming language. Example 1 String = “Tutorialspoint” Output = The repeating character is “t” In the above example, the leftmost character of the input ... Read More

Advertisements