Found 7197 Articles for C++

Making a Palindrome pair in an array of words (or strings) in C++

Prateek Jangid
Updated on 07-Mar-2022 07:45:56

557 Views

"Madam" or "racecar" are two words that read the same backward as forwards, called palindromes.If we are given a collection or list of strings, we have to write a C++ code to find out if that can join any two strings in the list together to form a palindrome or not. If any such pair of strings exist in the given list, we have to print "Yes, " else we have to print "No."In this tutorial, input will be an array of strings, and output will be a string value accordingly, for exampleInputlist[] = {"flat", "tea", "chair", "ptalf", "tea"}OutputYesThere is ... Read More

Largest BST in a Binary Tree in C++

Prateek Jangid
Updated on 07-Mar-2022 07:35:19

458 Views

In a binary tree, there are only two nodes (left and right) in each child node. Tree structures are simply representations of data. Binary Search Trees (BSTs) are special types of Binary Trees that meet these conditions −Compared to its parent, the left child node is smallerThe parent node of the right child is larger than the child nodeSuppose we're given a binary tree, and we are supposed to find out what's the largest binary search tree (BST) within it.In this task, we will create a function to find the largest BST in a binary tree. When the binary tree ... Read More

Kruskal's Minimum Spanning Tree Algorithm-Greedy algorithm in C++

Prateek Jangid
Updated on 07-Mar-2022 07:27:02

4K+ Views

A spanning tree is a linked and undirected graph subgraph that connects all vertices. Many spanning trees can exist in a graph. The minimum spanning tree (MST) on each graph is the same weight or less than all other spanning trees. Weights are assigned to edges of spanning trees and the sum is the weight assigned to each edge. As V is the number of vertices in the graph, the minimum spanning tree has edges of (V - 1), where V is the number of edges.Finding minimum spanning tree using Kruskal’s algorithmAll of the edges should be arranged in a ... Read More

Reverse Alternate K Nodes in a Singly Linked List in C++

Prateek Jangid
Updated on 07-Mar-2022 07:02:41

330 Views

In this tutorial, we are given a linked list A of length N and an integer K. We have to reverse alternate pairs of nodes with the size of each pair as K. It is also given that N is divisible by K. First argument is the head pointer of the linked list A and the second argument is an integer K, for exampleInput5 -> 6 -> 2 -> 8 -> 5 -> 2 -> 4 -> 8 -> 9 -> 6 -> null K=2Output6 -> 5 -> 2 -> 8 -> 2 -> 5 -> 4 -> 8 -> ... Read More

Number of substrings divisible by 8 and not by 3 in C++

Prateek Jangid
Updated on 07-Mar-2022 06:52:37

232 Views

A string of 0-9 is given. For this problem, we need to calculate the number of strings that are divisible by 8 and not by 3. This is a 2 step problem, and we need to do the code one step at a time to solve it, for exampleInputstr = "80"Output2Inputstr = "7675636788"Output15Approach to Find the SolutionOnly numbers with their last 3 digits are divisible by 8, and their sum of digits divisible by 3 are divisible by 8.Now store the prefix sum of the string so that the sum of digits of prefix module 3 is either 0, 1, ... Read More

Number of Substrings divisible by 6 in a String of Integers in C++

Prateek Jangid
Updated on 07-Mar-2022 06:47:16

362 Views

We'll look at a problem in which we're given an integer string and must determine how many substrings are divisible by 6 in integer format. It should be noted that input is in the form of a String made of numbers (integers). Still, the divisibility check will be performed considering it as an integer only (not using ASCII value of string input).Inputstr = “648”Explanationsubstring “6”, “48”, and “648” are divisible by 6.Inputstr = “38342”Output4Explanationsubstrings “3834”, “342”, ”834”, and “42” are divisible by 6.Brute-Force ApproachUsers can check every possible substring to see if it's divisible by six. If the substring is ... Read More

Passing the Assignment in C++

Prateek Jangid
Updated on 07-Mar-2022 06:33:34

178 Views

In this tutorial, we have to write an algorithm to find a way to pass an assignment without being caught by the invigilator. Each student has to submit their assignment to the invigilator. Student A's assignment is with Student B, so Student B has to return/pass the assignment to Student A without the invigilator noticing them.All the students are sitting in a queue. We need to find a way to pass the assignment back to Student A without being caught. Various requirements under which they can pass assignments are as follow −Student A (At index i) can pass assignments to ... Read More

C++ program to find minimum how much rupees we have to pay to buy exactly n liters of water

Arnab Chakraborty
Updated on 04-Mar-2022 07:12:40

222 Views

Suppose we have three numbers n, a and b. We want to buy n liters of water. There are only two types of water bottles nearby, 1-liter bottles and 2-liter bottles. The bottle of the first type a rupees and the bottle of the second type costs b rupees. We want to spend as few money as possible. We have to find the minimum amount of money we need to buy exactly n liters of water.So, if the input is like n = 7; a = 3; b = 2, then the output will be 9, because with 3 2-liter ... Read More

C++ Program to find minimum possible ugliness we can achieve of towers

Arnab Chakraborty
Updated on 04-Mar-2022 07:09:47

193 Views

Suppose we have an array A with n elements. Consider there are n block towers in a row. The ith tower has height A[i]. In a single day, we can perform the operation: Select two indices i and j (i != j) and move back from tower i to j. It will decrease A[i] by 1 and increase A[j] by 1. The ugliness of the buildings is max(A) − min(A). We have to find the minimum possible ugliness we can achieve.So, if the input is like A = [1, 2, 3, 1, 5], then the output will be 1, because ... Read More

C++ Program to find room status after checking guest appearance record

Arnab Chakraborty
Updated on 04-Mar-2022 07:06:54

333 Views

Suppose we have a string S with 'L', 'R' and digits from 0 to 9. Consider there is a hotel with 10 rooms they are numbered from 0 to 9, from left to right. The hotel has two entrances-one from the left side, and another from the right side. When a customer arrives to the hotel through the left entrance, they get an empty room closest to the left entrance. Similarly, when a customer arrives at the hotel through the right entrance, they get an empty room closest to the right entrance. But we have lost the room assignment list. ... Read More

Advertisements