Found 7197 Articles for C++

Find the K Closest Points to Origin using Priority Queue

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

251 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

123 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

103 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

695 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

302 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

138 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

How to validate IFSC Code using Regular Expression?

Sakshi Koshta
Updated on 01-Aug-2023 19:48:16

2K+ Views

Indian Financial System Code is the abbreviation. Indian bank branches that take part in the electronic fund transfer system are identified by a special 11-character code. The Reserve Bank of India uses this code to transfer money between banks during internet transactions. There are two sections to the IFSC code. The bank is identified by the first four characters, whereas the branch is identified by the final six characters. NEFT (National Electronic Funds Transfer), RTGS (Real Time Gross Settlement), and IMPS (Immediate Payment Service) are just a few of the electronic transactions that require IFSC codes. Method Some general methods ... Read More

Print all the non-repeating words from the two given sentences

Sakshi Koshta
Updated on 01-Aug-2023 19:45:56

354 Views

In this tutorial, we will identify and print all non-repeating words from two given sentences. Non-repeating words refer to words that appear only once in both sentences, means they do not repeat in other sentence. The task involves analysis of the input sentences, identification of the individual words, and comparing them across both sentences to find which ones appear only once. The output should be a list of all such words. This task can be accomplished through various programming approaches, such as using loops, arrays, or dictionaries. Methods Here are the two methods to print all the non-repeating words from ... Read More

Permutation of a string with maximum number of characters greater than its adjacent characters

Sakshi Koshta
Updated on 01-Aug-2023 19:43:18

390 Views

It is crucial to manipulate strings in various problem-solving scenarios. Discovering a permutation of the given string that optimizes the count of characters larger than their contiguous counterparts presents an enjoyable puzzle, requiring rearranging the string's characters to generate as many pairs as possible of adjacent characters where the left character is lesser than the right. Methods There are several methods to solve permutations of a string where maximum number of characters is more than characters immediately adjacent to them. Method 1 − Backtracking with Pruning − Method 2 − Dynamic Programming− Method 3 − Heap's Algorithm− Method 4 ... 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