Found 1860 Articles for Data Structure

Longest Subarray whose Elements can be Made Equal by Maximum K Increments

Shubham Vora
Updated on 02-Aug-2023 13:40:21

444 Views

In this problem, we will find the length of the longest subarray so that we can make all subarray elements the same by adding some positive integer values, and the sum of added numbers to each element shouldn’t increase than K. The naïve approach is to find the cost of making all elements the same in each subarray of the given array. Finally, consider the length of the subarray whose cost is less than K and the length is maximum. However, we will use the queue data structure to solve the problem efficiently. Problem statement − We have given an ... Read More

Implementation of a Hypergraph

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

312 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

157 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

252 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

128 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

104 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

704 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

304 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

145 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

1K+ 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

Advertisements